0

我从我的 删除行时遇到问题tableView,因为我有多个部分是在视图控制器出现时动态生成的,所以当我返回计数时,numberOfRowsInSection它最终看起来像这样:

NSInteger count = [[_sectionsArray objectAtIndex:section] count];
return count;

现在,当删除时,我会生成相同类型的数组,如下所示:

NSMutableArray *contentsOfSection = [[_sectionsArray objectAtIndex:[indexPath section]] mutableCopy];
[contentsOfSection removeObjectAtIndex:[indexPath row]];

如您所见,我正在从未链接到的数组中删除对象tableView,因此它只返回一个NSInternalInconsistencyException

有人可以帮我吗?

更新:

    [contentsOfSection removeObjectAtIndex:[pathToCell row]];

    if ([contentsOfSection count] != 0) {
        // THIS DOESN'T
        [self.tableView deleteRowsAtIndexPaths:@[pathToCell] withRowAnimation:UITableViewRowAnimationFade];
    }
    else {
        // THIS WORKS!
        [_sectionsArray removeObjectAtIndex:[pathToCell section]];
        [self.tableView deleteSections:[NSIndexSet indexSetWithIndex:[pathToCell section]] withRowAnimation:UITableViewRowAnimationFade];
    }
4

2 回答 2

1

muatableCopy 将创建数组的另一个实例。因此,您要从新创建的数组中删除项目,而不是从旧数组中删除项目。始终将“contentsOfSection”作为可变数组存储在 _sectionsArray 中。然后像这样删除。

NSMutableArray *contentsOfSection = [_sectionsArray objectAtIndex:[indexPath section]];
[contentsOfSection removeObjectAtIndex:[pathToCell row]];

if ([contentsOfSection count] != 0) {

    [self.tableView deleteRowsAtIndexPaths:@[pathToCell] withRowAnimation:UITableViewRowAnimationFade];
}
else {
    // THIS WORKS!
    [_sectionsArray removeObjectAtIndex:[pathToCell section]];
    [self.tableView deleteSections:[NSIndexSet indexSetWithIndex:[pathToCell section]] withRowAnimation:UITableViewRowAnimationFade];
}
于 2013-02-02T13:00:17.570 回答
0

在以下代码中:

[_sectionsArray removeObjectAtIndex:[pathToCell section]];
[self.tableView deleteSections:[NSIndexSet indexSetWithIndex:[pathToCell section]] withRowAnimation:UITableViewRowAnimationFade];

You are deleting object from _sectionArray. So this array automatically updates. But in other cases you are creating another copy and then deleting object from that array. So your _sectionArray is not going to update. So this is necessary that after deleting object from copy array, update section array with that new array also.

NSMutableArray *contentsOfSection = [[_sectionsArray objectAtIndex:[indexPath section]] mutableCopy];
[contentsOfSection removeObjectAtIndex:[indexPath row]];
[_sectionsArray replaceObjectAtIndex:[indexPath section] withObject:contentsOfSection];

Try this one and I hope that this will work.

于 2013-02-02T14:03:29.447 回答