尝试从一个部分删除行时,UITableView numberOfRowsInSection返回0

时间:2016-01-13 18:04:45

标签: ios objective-c uitableview

我有一个包含两个部分的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" - 在控制台中打印

0 个答案:

没有答案