繁体   English   中英

UITableView-删除节中的最后一个单元格

[英]UITableView- deleting last cell in section

这是删除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
    }
}

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM