我有一个包含两个部分的tableview,我试图从一个部分删除一行并插入下一个部分。以下是我的表现,
[[PickDataController sharedDataController].picks removeObject:pick];
NSIndexPath *removeIndexPath = indexPath;
[[PickDataController sharedDataController].completePicks insertObject:pick atIndex:0];
NSIndexPath *insertIndexPath = [NSIndexPath indexPathForRow:0 inSection:1];
NSLog(@"numberOfRowsInSection: %ld", (long)[self.tableView numberOfRowsInSection:0]);
[self.tableView beginUpdates];
[self.tableView deleteRowsAtIndexPaths:@[removeIndexPath] withRowAnimation:UITableViewRowAnimationFade];
[self.tableView insertRowsAtIndexPaths:@[insertIndexPath] withRowAnimation:UITableViewRowAnimationFade];
[self.tableView endUpdates];
插入和删除大部分时间都会发生,但有时应用程序会与消息崩溃,
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'attempt to delete row 0 from section 0 which only contains 0 rows before the update'
这是我的numberofRowsinSection方法,
(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if(section == 0) {
return [PickDataController sharedDataController].picks.count;
} else {
return [PickDataController sharedDataController].completePicks.count;
}
}
当发生这种情况时," numberOfRowsInSection:0" - 在控制台中打印