0

我正在尝试使用 insertRowsAtIndexPaths 以便可以插入表格行。我收到此错误:

*** 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 (0) must be equal to the number of rows contained in that section before the update (0), plus or minus the number of rows inserted or deleted from that section (1 inserted, 0 deleted).'

我正在尝试做的是动态修改部分中的行数以尝试纠正问题:

NSArray *indexPathIndex = [NSArray arrayWithObjects:[NSIndexPath indexPathForRow:0   inSection:0], nil];    
[myTableView beginUpdates];
//***** HERE I AM TRYING TO DYNAMICALLY INCREASE NUMBER OF ROWS
[myTableView numberOfRowsInSection:1];
////////////////////////////////////////////////////////////
[myTableView insertRowsAtIndexPaths:indexPathIndex   withRowAnimation:UITableViewRowAnimationTop];
[myTableView endUpdates];

最初,我在节中的行数是 0。我尝试增加这个数字以解决上述错误,但是代码 [myTableView numberOfRowsInSection:1] 似乎没有效果。

有人可以建议我如何解决这个问题吗?

谢谢你,维克多。

4

3 回答 3

0

看看UITableViewDataSource协议,特别是tableView:numberOfRowsInSection方法。这将允许您动态设置节中的行数。

于 2011-06-24T16:30:41.873 回答
0

您不应该自己设置行数,UITableView 会自行管理。删除线。

myTableView numberOfRowsInSection:1]; 
于 2011-06-24T16:14:10.340 回答
0

tableView 有一个数据源,通常是一个数组,您可以使用它来填充 tableView 的单元格。

- (UITableViewCell*)tableView cellForRowAtIndexPath..... {
    //Dequeueing or alloc-init-ing a cell
    SomeClass *someObject = [someArray objectAtIndex:indexPath.row]; // Something
    // like this, where someArray becomes the source array.
    ...
}

现在在你的代码的其他部分,如果你想动态改变tableView的内容,改变tableViewsomeArray然后再改变。

[someArray insertObjectAtIndex:someIndex];
[myTableView insertRowsAtIndexPaths....];  //The indexPath is generally
// consistent with the someIndex.
于 2011-06-24T18:26:13.437 回答