删除UITableView的最后一部分会导致exception

由于未捕获的exception’NSInternalInconsistencyException’而终止应用程序,原因:’UITableView内部错误:无法生成具有旧节数的新节图:1和新节数:0′

以下是代码

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { @try { NSMutableArray *arrData = [dictStartDate* objectForKey:self.navigationItem.title]; NSLog(@"Title : %@", self.navigationItem.title); NSLog(@"Array count : %i", (int)arrData.count); return arrData.count; } @catch (NSException *ex) { [[ProjectHandler sharedHandler]LogException:ex.name description:ex.description className:@"TodayTasksViewController" functionName:@"numberOfRowsInSection"]; return 0; } } 

我遇到了这个问题,如果我要删除一个部分的最后一行,我就解决了测试问题,如果是这样,我删除了该部分。 我返回0到numberOfSectionsInTableView并且没有问题,tableView只是空的。

Bellow遵循我用来检查它是否是段的最后一行的快速代码和关键部分,即删除该部分而不是行。

假设您在表视图中有对象的对象,如下所示:

 var objects: [[Object]]? 

然后,如果要使用此对象删除此表的一行,则应执行以下操作:

 objects![indexPath.section].removeAtIndex(indexPath.row) if (objects![indexPath.section].count == 0) { objects!.removeAtIndex(indexPath.section) tableView.deleteSections(NSIndexSet.init(index: indexPath.section), withRowAnimation: .Left) return //End the func and not execute the next step } tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Left) 

你也可以用这样的东西来识别它是否是一节中的最后一行:

 tableView.numberOfRowsInSection(indexPath.section) 

您似乎正在删除表中的唯一部分。 一张桌子必须至少有一个部分。 您可能需要检查代码以检查numberOfSectionsInTableView:是否未返回0 numberOfSectionsInTableView: