我们如何在TableView中的两个给定单元格之间动态添加新单元格?

时间:2012-05-12 13:15:51

标签: ios xcode ipad uitableview

我需要在选择表格的单元格时添加新单元格。新单元格应低于所选单元格。我们能做到吗?

以下是我创建4个单元格并希望在2到3之间插入单元格的代码。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
cell= [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];


if(indexPath.row == 0){

    cell.textLabel.text = @"My Tweets";
    cell.textLabel.textColor = [UIColor whiteColor];
    cell.backgroundColor = [UIColor blackColor];
    cell.imageView.image = [UIImage imageNamed:@"tweets.png"];
}

if(indexPath.row == 1){

    cell.textLabel.text = @"Search";
    cell.backgroundColor = [UIColor blackColor];

    cell.textLabel.textColor = [UIColor whiteColor];
    cell.imageView.image = [UIImage imageNamed:@"search.png"];
}

if(indexPath.row == 2){

    cell.textLabel.text = @"Followers";
    cell.textLabel.textColor = [UIColor whiteColor];
    cell.backgroundColor = [UIColor blackColor];
    cell.imageView.image = [UIImage imageNamed:@"followers.png"];
}

if(indexPath.row == 3){

    cell.textLabel.text = @"Following";
    cell.textLabel.textColor = [UIColor whiteColor];
    cell.backgroundColor = [UIColor blackColor];
    cell.imageView.image = [UIImage imageNamed:@"following.png"];
}



return cell;

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

//here I have to insert a new cell below on the click this existing cell.


if(indexPath.row == 1)
{

    NSIndexPath *newIndexPath = [NSIndexPath indexPathForRow:indexPath.row+1 inSection:0];
    NSArray *indexPaths = [NSArray arrayWithObject:newIndexPath]; 

    [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPaths] withRowAnimation:UITableViewRowAnimationAutomatic];
}

}

3 个答案:

答案 0 :(得分:1)

您应该更新数据源,然后调用此UITableView方法:

- (void)insertRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation

这将调用

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 

表视图数据源的方法,并使用您选择的动画更新您的表。

您不会像在发布的代码中那样在didSelectRowAtIndexPath方法中创建新单元格。


编辑添加信息。

要设置indexPaths数组(根据您的情况),您可以执行以下操作:

NSInteger row = [indexPath row];
NSInteger section = [indexPath section];    
NSIndexPath *newIndexPath = [NSIndexPath indexPathForRow:row+1 inSection:section];
NSArray *indexPaths = [NSArray arrayWithObject:indexPath]; 

答案 1 :(得分:1)

关于你的问题,在indexpath上创建数组时不要做indexpath.row + 1 然后在tableview上调用insertrowatindexpath ......... aloso reload table,你就完成了。

您可以添加此方法以向表格视图添加行

    - (void)setEditing:(BOOL)editing animated:(BOOL)animated
    {
        [super setEditing:editing animated:animated];

    NSArray *paths = [NSArray arrayWithObject:
                            [NSIndexPath indexPathForRow:1 inSection:0]];
        if (editing)
        {
            [[self tableView] insertRowsAtIndexPaths:paths
                                    withRowAnimation:UITableViewRowAnimationTop];
        }
        else {
           //Your Operation [[self tableView] deleteRowsAtIndexPaths:paths
                                    withRowAnimation:UITableViewRowAnimationTop];
        }
[tableView reloadData];
    }

如果你有UITableview的子类,你可以使用上面的方法,否则通过tutorial

还要提一下您要插入行的事件。只需插入行即可实现以下逻辑: -

为路径创建数组: -

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];

你必须给出之前的行号,然后你要插入行和节,然后在tableview上调用方法insertRowsAtIndexPaths:withRowAnimation:并重新加载它。

答案 2 :(得分:1)

只需在开始更新和结束更新块中调用insertrowsatindexpath委托。例如,尝试这样:

[tableViewForFinishRace beginUpdates];
        NSIndexPath *newIndexPath = [NSIndexPath    indexPathForRow:arrayForTitle.count-1 inSection:0];
        NSLog(@"index path for insertion =%@",newIndexPath);
        [tableViewForFinishRace insertRowsAtIndexPaths:@[newIndexPath] withRowAnimation:UITableViewRowAnimationTop];
        [arrayForTitle insertObject:@"Gearing Up for a" atIndex:arrayForTitle.count-1];
        [arrayForImage insertObject:@"gearing-up-icon.jpeg" atIndex:arrayForTitle.count-2];
[tableViewForFinishRace endUpdates];