且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何删除一个部分的最后一行?

更新时间:2023-12-03 15:08:22

当你删除了一行,并且这一行是其section的最后一个,你需要同时删除这个section.基本上,您需要跟踪要删除的与行关联的 indexPaths 以及与需要删除的部分相关的索引,因为它们不再包含行.你可以这样做:

When you delete a row, and this row is the last one of its section, you need to also delete the section. Basically, you need to keep track of both the indexPaths you want to delete, which are associated to the rows, and the indexes related to the sections that needs to be removed because they no longer contain rows. You could do it as follows:

NSMutableIndexSet *indexes = [NSMutableIndexSet indexSet];

每次从模型数组中删除与 tableView 的特定部分相关的对象时,检查数组计数是否为零,在这种情况下,将表示该部分的索引添加到索引:

Each time you delete an object from your model array related to a specific section of the tableView, check if the array count is zero, in which case add an index representing the section to indexes:

[array removeObjectAtIndex:indexPath.row];
if(![array count])
    [indexes addIndex: indexPath.section];

确定所有与要删除的行相关的indexPath,然后更新tableView如下:

Determine all of the indexPaths related to the rows to be deleted, then update the tableView as follows:

[tableView beginUpdates];
[tableView deleteRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationFade];
[tableView deleteSections:indexes withRowAnimation:UITableViewRowAnimationFade];
[tableView endUpdates];

这对我和我建议该方法的其他人都有效.

This worked for me and other people I suggested the approach.