【问题标题】:UITableView- deleting last cell in sectionUITableView-删除部分中的最后一个单元格
【发布时间】:2012-01-28 05:48:46
【问题描述】:

这是我在 UITableView 中删除单元格的代码:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView beginUpdates];

    if([tableView numberOfRowsInSection:[indexPath section]] > 1) {
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] 
            withRowAnimation:UITableViewRowAnimationFade];
    } else {
        [tableView deleteSections:[NSIndexSet indexSetWithIndex:[indexPath section]] 
            withRowAnimation:UITableViewRowAnimationFade];
    }

    [tableView endUpdates];
}

在删除单元格之前还有一些数据正在更新,但我很确定它正在正确更新,因为每次删除单元格时,数据源数组的count 总是少一个。这是预期的行为。然而由于某种原因,每当我删除部分中的最后一个单元格时,我都会得到'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 (1), plus or minus the number of rows inserted or deleted from that section (0 inserted, 0 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).'

这非常令人沮丧,尤其是因为从我在 Internet 上能够找到的信息来看,我这样做是正确的。也许我需要睡觉了,已经有一段时间了。这里有什么想法吗?

解决方案:

- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section {
    if(tableIsEmpty) {
        //**this** line was the culprit- returning zero here fixed the problem
        return 0;
    } else if(section == ([tableView numberOfSections] - 1)) {
        //this is the last section- it only needs one row for the 'Delete all' button
        return 1;
    } else {
        //figure out how many rows are needed for the section
    }
}

【问题讨论】:

    标签: ios uitableview delete-row


    【解决方案1】:

    您的问题在于您的numberOfRowsInSection: 方法的实现。您的删除似乎没问题。删除行后,我认为您无法更新用于在 numberOfRowsInSection: 方法中返回行数的源。由于那里的不一致,您会收到错误消息。请分享该代码。

    【讨论】:

    • 是的,是的!你完全正确。我想我需要的只是有人告诉我我的问题不是与我的删除代码有关。我把它理顺了——这与我总是用“全部删除”单元格显示一个额外的部分有关,当最后一个部分被删除时,我并没有让它消失。总之,你帮了很多忙,谢谢!
    • @Rickay 很高兴能帮上忙。
    猜你喜欢
    • 1970-01-01
    • 2012-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-27
    • 1970-01-01
    • 1970-01-01
    • 2011-05-11
    相关资源
    最近更新 更多