NSInternalInconsistencyException',原因:'尝试将第0行插入第0部分,但更新后第0部分只有0行'

ins*_*iac 40 objective-c uitableview ios

我正在使用UITableViewController并在更新tableView时收到此错误.以下是我的代码:

当我执行click事件时会发生这种情况:

[timeZoneNames insertObject:@"HELLO" atIndex:0];  
[self.tableView beginUpdates];   
NSArray *insertIndexPaths = [NSArray arrayWithObject:[NSIndexPath indexPathForRow:0 inSection:0]];
[self.tableView insertRowsAtIndexPaths:insertIndexPaths withRowAnimation:UITableViewRowAnimationTop];
[self.tableView endUpdates];
Run Code Online (Sandbox Code Playgroud)

我试图寻找苹果文档,但没有帮助.

谢谢,阿比

Nia*_*lJG 31

当我忘记正确设置数据源插座时,我遇到了这个错误.

如果看到此错误,请检查是否已显式设置TableView委托和数据源:

  • 转到界面构建器并使用"表视图"查看视图

  • cmd +右键单击拖动(您应该看到一条蓝线)从"表视图"图标到文件的标题图标,它在xcode 6旁边有一个黄色图标.

  • 释放鼠标,您应该看到委托和数据源选项.

  • 选择'数据源'

你的桌子现在应该正确连线了.


Jef*_*mas 28

我之前遇到过这个问题.这意味着,当-insertRowsAtIndexPaths:withRowAnimation:被称为-tableView:numberOfRowsInSection:返回0.你需要插入到模型打电话之前-insertRowsAtIndexPaths:withRowAnimation:(见-beginUpdates)


更新

我想知道返回值-tableView:numberOfRowsInSection:是多少?此外,如果您只有一个更新,则不需要-beginUpdates/ -endUpdates.

[timeZoneNames insertObject:@"HELLO" atIndex:0];
// Let's see what the tableView claims is the the number of rows.
NSLog(@"numberOfRowsInSection: %d", [self tableView:self.tableView numberOfRowsInSection:0]);
NSArray *insertIndexPaths = [NSArray arrayWithObject:[NSIndexPath indexPathForRow:0 inSection:0]];
[self.tableView insertRowsAtIndexPaths:insertIndexPaths withRowAnimation:UITableViewRowAnimationTop];
Run Code Online (Sandbox Code Playgroud)

  • @Aby 来自`-beginUpdates` 文档:“如果您希望同时对后续插入、删除和选择操作(例如,cellForRowAtIndexPath: 和 indexPathsForVisibleRows)进行动画处理,请调用此方法。” 它用于将多个 UI 操作组合为一个 UI 操作:您只有一个操作。 (2认同)