【发布时间】:2016-08-31 09:58:23
【问题描述】:
我在打电话
performBatchUpdates({
self.insertItemsAtIndexPaths(indexPathes)
},
completion: {
_ in
})
在控制器中不可见的集合视图上!已经有另一个视图控制器推到它上面。
这会导致以下错误:
Invalid update: invalid number of items in section 0. The number of items contained in an existing section after the update (12) must be equal to the number of items contained in that section before the update (12), plus or minus the number of items inserted or deleted from that section (12 inserted, 0 deleted) and plus or minus the number of items moved into or out of that section (0 moved in, 0 moved out).'
但这根本不是真的,因为如果我在视图可见时调用 performBatchUpdates 它将完美运行。
到底发生了什么?
编辑: 请注意,如果我调用 reloadData 它工作正常,而视图不可见。苹果对此有什么看法?
我怎样才能捕捉到这个异常。所以我可以用 reloadData 代替?
编辑: 我在调用 performBachUpdates 之前打印了以下内容:
print("visibleCells().count :" + String(self.visibleCells().count))
print("dataModel count:" + String(self.dataArray.count))
这是我得到的:
visibleCells().count :0
dataModel count:12
表示异常不成立!!
【问题讨论】:
-
您需要更新数据模型以匹配您插入到集合视图中的项目。
-
@rmaddy 谢谢你的评论。我在调用 insetItemIndexPath 之前更新了数据模型。
-
@rmaddy 这很好用,但是视图控制器应该是可见的!!
-
您应该仅在视图可见时更新它。那就是当您弹回视图控制器时,您正在执行此批量更新。尝试在视图中执行它。
-
@AbinGeorge 感谢您的回复,我使用 reloadData 功能。它为什么有效?苹果在文档中提到过吗
标签: ios uicollectionview ios9