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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10564087/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 00:17:40  来源:igfitidea点击:

How can we dynamically add new cell between two given cell in TableView?

iosxcodeipaduitableview

提问by Nihal Sharma

I need to add a new cell on selection of a cell of table. The new cell should come below the selected cell. Can we do it?

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

Below is the code where I am creating 4 cells and want to insert a cell between 2 and 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];
}

}

}

回答by Darren

You should update your data source and then call this UITableView method:

你应该更新你的数据源,然后调用这个 UITableView 方法:

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

This is going to call the

这将调用

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

method of the table view data source, and update your table, with the animation that you choose.

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

You would not create the new cell in the didSelectRowAtIndexPath method as you are doing in the code that you posted.

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



edited to add info.

编辑以添加信息。

To set the indexPaths array (for your case) you can do this:

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

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

回答by Abhishek Singh

With respect to your question do not do indexpath.row+1 while creating array on indexpath and then call insertrowatindexpath on tableview.........aloso reload table and you are done.

关于你的问题,在 indexpath 上创建数组时不要做 indexpath.row+1 ,然后在 tableview 上调用 insertrowatindexpath .........也重新加载表,你就完成了。

You can add this method to add rows to your tableview

您可以添加此方法将行添加到您的 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];
    }

You can use the above method if you have subclassed UITableview else go through the tutorial

如果您已经继承了 UITableview ,则可以使用上述方法,否则请阅读教程

Also mention on what event you want to insert the rows.Just to insert row you can implement the following logic:-

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

Create array for paths:-

为路径创建数组:-

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

you have to give the previous row number after which you want to insert the row and the section and then you call the method insertRowsAtIndexPaths:withRowAnimation: on the tableview and reload it.

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

回答by wannaBeGeek

Just call the insertrowsatindexpath delegates within the begin update and end update block.For example try it like this:

只需在开始更新和结束更新块中调用 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];