如何在TableView的两个特定单元格之间动态添加新单元格?

3

当选中表格单元格时,我需要添加一个新的单元格。新单元格应该位于所选单元格下方。我们可以这样做吗?

以下是我创建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];
}

}


1
使用 UITableView-beginUpdates-endUpdates-insertRowsAtIndexPaths:withRowAnimation: 方法。 - Christian Schnorr
嘿,我正在为同样的问题苦苦挣扎。你找到任何解决方案了吗?我知道已经过了很长时间,但你能帮帮我吗? - Bella
3个回答

1

只需在 begin update 和 end update 块内调用 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];

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]; 

IndexPath和IndexPath有什么关系? - Nihal Sharma
indexPaths只是一个indexPath数组。我会更新我的答案,并提供一个在你的情况下应该有效的示例。 - Darren
出现错误 - 由于未捕获的异常“NSInternalInconsistencyException”,程序终止,原因是“无效更新:在第0部分中的行数无效。更新后现有部分中包含的行数(5)必须等于更新前该部分中包含的行数(5)加上或减去从该部分中插入或删除的行数(1插入,0删除),以及移入或移出该部分的行数(0移入,0移出)的数量。” - Nihal Sharma
看起来你没有更新数据源以添加额外的行。你的表视图数据源tableView:numberOfRowsInSection方法中有什么? - Darren
你没有添加 numberOfRowsInSection 方法的代码。这很可能是你发布错误的原因。然而,以你的代码书写方式,你永远无法插入单元格,因为你的单元格是硬编码的。你的单元格数据应该在一个数组中,其中索引对应每一行。你需要更新 cellForRowAtIndexPath 方法来从此数组中填充单元格。在 didSelectCellAtIndexPath 方法中,你将更新此数组并调用 insertRowAtIndexPath 方法。 - Darren

1
关于您的问题,在创建数组时不要使用indexpath.row+1,然后在tableview上调用insertrowatindexpath……同时重新加载表格即可完成。
您可以添加此方法来向您的tableview添加行。
    - (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];

您需要提供要插入行的前一行号码和部分,然后在表视图上调用方法insertRowsAtIndexPaths:withRowAnimation:并重新加载它。


我认为这并没有帮助,因为我必须在其中添加一行新行。我肯定会尝试的。 - Nihal Sharma
是的!它只在最后添加了一行新的数据。 - Nihal Sharma
假设你有8行数据,需要在第6行后添加新的数据,那么你需要创建一个IndexPath对象,其row为6,section为0。同时,你还需要将didSelectRowAtIndexPath方法中的if(indexpath.row==1)条件语句删除。如果问题解决了,请回复我。 - Abhishek Singh
其实我已经做了。尝试只是使用您的函数,但行被添加在最后。 - Nihal Sharma
NSInteger section = 0; NSInteger row = 5; NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:section]; 你已经转换成NSARRAY了吗? NSArray *indexPathsToInsert = [NSArray arrayWithObject:indexPath];[[self mainTableView] insertRowsAtIndexPaths:indexPathsToInsert withRowAnimation:UITableViewRowAnimationRight]; - Abhishek Singh
我不知道它是否适用于您。但这里的代码是将一行添加到最后一个索引。 - Nihal Sharma

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接