【问题标题】:How can we dynamically add new cell between two given cell in TableView?我们如何在 TableView 的两个给定单元格之间动态添加新单元格?
【发布时间】:2012-05-20 19:14:12
【问题描述】:

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

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

}

【问题讨论】:

  • 使用UITableViews -beginUpdates-endUpdates-insertRowsAtIndexPaths:withRowAnimation:
  • 嘿,我正在努力解决同样的问题。你有什么解决办法吗?我知道这已经很长时间了,但你能帮我吗

标签: ios xcode ipad uitableview


【解决方案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]; 

【讨论】:

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

关于您的问题,在 indexpath 上创建数组时不要执行 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];

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

【讨论】:

  • 我认为这无济于事,因为我必须在两者之间添加一个新行。一定会尝试的。
  • 是的!它只在最后添加了一个新行。
  • 说你有 8 行,你必须在第 6 行之后添加,然后为第 6 行和第 0 部分创建索引路径。同时从 didselectRowAtIndexPath 中删除条件 if(indexpath.row==1)...revert back如果解决了:)
  • 实际上是这样做的。尝试使用您的函数,但最后添加了该行。
  • NSInteger 部分 = 0; NSInteger 行 = 5; NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:section];你转换成NSARRAY了吗??? NSArray *indexPathsToInsert = [NSArray arrayWithObject:indexPath]; [[self mainTableView] insertRowsAtIndexPaths:indexPathsToInsert withRowAnimation:UITableViewRowAnimationRight];
【解决方案3】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-10-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多