[英]Dynamic array returning count in numberOfRowsInSection
I have problem with deleting rows from my tableView
, because I have multiple sections that are dynamically generated when view controller appears, so when I return the count in numberOfRowsInSection
it ends up looking like this: 我从
tableView
删除行时遇到问题,因为当视图控制器出现时,我会动态生成多个节,因此当我在numberOfRowsInSection
返回计数时,它最终看起来像这样:
NSInteger count = [[_sectionsArray objectAtIndex:section] count];
return count;
Now when deleting I generate this same type of array like this: 现在,当删除时,我将生成相同类型的数组,如下所示:
NSMutableArray *contentsOfSection = [[_sectionsArray objectAtIndex:[indexPath section]] mutableCopy];
[contentsOfSection removeObjectAtIndex:[indexPath row]];
As you can see I'm deleting the object from an array that is not linked to the tableView
, so it just returns an NSInternalInconsistencyException
如您所见,我正在从未链接到
tableView
的数组中删除对象,因此它仅返回NSInternalInconsistencyException
Can anybody help me with this? 有人可以帮我吗?
UPDATE: 更新:
[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];
}
muatableCopy will create another instance of the array. muatableCopy将创建该数组的另一个实例。 So you are removing item from the newly created array not from the old one.
因此,您要从新创建的数组中删除项目,而不是从旧数组中删除项目。 Always store 'contentsOfSection ' as a mutable array in _sectionsArray.
始终将“ contentsOfSection”作为可变数组存储在_sectionsArray中。 Then remove like this.
然后像这样删除。
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];
}
In following code : 在以下代码中:
[_sectionsArray removeObjectAtIndex:[pathToCell section]];
[self.tableView deleteSections:[NSIndexSet indexSetWithIndex:[pathToCell section]] withRowAnimation:UITableViewRowAnimationFade];
You are deleting object from _sectionArray. 您正在从_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.
因此,您的_sectionArray不会更新。 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. 试试这个,我希望它能起作用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.