Iphone 在uitableview的同一节中插入/创建新行

Iphone 在uitableview的同一节中插入/创建新行,iphone,uitableview,insert,row,nsindexpath,Iphone,Uitableview,Insert,Row,Nsindexpath,我陷入了一个问题,我有一个行的uitableview,比如说5。 如果用户选择一行,则应使用动画创建/插入恰好位于已点击行下方的新行(正如我们在隐藏/取消隐藏部分中所看到的),并在点击新插入行时将其删除 我试过了,但它说 Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section

我陷入了一个问题,我有一个行的uitableview,比如说5。 如果用户选择一行,则应使用动画创建/插入恰好位于已点击行下方的新行(正如我们在隐藏/取消隐藏部分中所看到的),并在点击新插入行时将其删除

我试过了,但它说


Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid  
update: invalid number of rows in section 0.  The number of rows contained in an existing section 
after the update (6) must be equal to the number of rows contained in that section before the update 
(5), plus or minus the number of rows inserted or deleted from that section (0 inserted, 0 deleted).'
那么,实现这一功能的另一种方法是什么呢?
提前感谢。

最初您有5行。将新行添加到表中,比如使用addRowsAtIndexPaths:method。此时,您的表视图将调用其数据源方法,因为它需要添加这个新单元

但是,可能您仍然从datasource方法返回5行(而不是6行),这导致了不一致性(因为table view需要6行,而您仍然返回5行)

因此,假设当表视图为新创建的单元格(行=5)调用cellforrowatinexpath:method时,它可能会崩溃,因为您必须执行以下操作:

[yourDatasourceArray objectAtIndex:indexPath.row];
上面的语句将导致崩溃,因为indexath.row是5,而数组中仍然有5个对象(索引0到4)。因此objectAtIndex:5导致崩溃


- (NSInteger)numberOfRowsInSection:(NSInteger)section {
    switch (section) {
        case 0:
            return numberOfRows;
    }
    return 0;
}

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

    numberOfRows ++;
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    NSMutableArray*  tempArray = [[NSMutableArray alloc] init];
        [tempArray addObject:[NSIndexPath indexPathForRow:indexPath.row +1  inSection:indexPath.section]];
    [tableView beginUpdates];
    [tableView insertRowsAtIndexPaths:tempArray withRowAnimation:UITableViewRowAnimationRight];
    [tableView endUpdates]; 
    [tempArray release];

}


我犯了两个错误 1) 我没有使用[tableView BeginUpdate],显然更新后也没有使用[tableView EndUpdate] 2) 计算新行的indexpath的方法模棱两可


非常感谢Pratikshabisikar和Max Howell投入您的时间和精力。

这很可能就是答案。总之,在调用
insert
delete
后,从
numberOfRows
返回正确的号码。嘿,伙计们。。。谢谢你的评论。。。我已经解决了这个问题。。。请在下面找到我的答案