dav*_*vid 5 ios uicollectionview ios9
我在打电话
performBatchUpdates({
self.insertItemsAtIndexPaths(indexPathes)
},
completion: {
_ in
})
Run Code Online (Sandbox Code Playgroud)
在控制器中不可见的集合视图上!已经有另一个视图控制器推到它上面。
这会导致以下错误:
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).'
Run Code Online (Sandbox Code Playgroud)
但这根本不是真的,因为如果我在视图可见时调用 performBatchUpdates,它将完美运行。
到底发生了什么?
编辑:请注意,如果我调用 reloadData,它可以在视图不可见时正常工作。苹果对此有何评论?
我怎样才能捕捉到这个异常。所以我可以改用 reloadData 吗?
编辑:我在调用 performBachUpdates 之前打印了以下内容:
print("visibleCells().count :" + String(self.visibleCells().count))
print("dataModel count:" + String(self.dataArray.count))
Run Code Online (Sandbox Code Playgroud)
这是我得到的:
visibleCells().count :0
dataModel count:12
Run Code Online (Sandbox Code Playgroud)
这意味着异常不是真的!
正如 @rmaddy 在评论中提到的,您的问题是您没有正确更新数据模型。
你调用insertItemsAtIndexPaths了indexPathes(其中包含12个索引),但是你的数据模型以12个索引开始,批量更新后以12个索引结束。仅当您在数据模型中插入某些内容时才需要调用 insert (例如,在调用类似于 的内容后dataModel.insert("whatever", atIndex: 0),您也可以insertItemsAtIndexPaths使用索引 0 进行调用)。
此处的情况似乎并非如此,因为最终得到的元素数量相同 (12)。在这种情况下,如果您想刷新集合视图,则需要调用reloadData(或者如果数据没有更改,则不要调用任何内容)
reloadData不会抛出此类异常,因为它不会添加/删除任何内容,它只会获取您在数据源中提供的任何内容。insertItemsAtIndexPaths可以对更改进行动画处理,因此它将执行一些检查以查看您的数据模型(无论您在数据源中提供什么)是否添加了新元素,否则抛出异常。
捕获异常(使用 @try/catch)不会让你走得太远,因为它会使更新处于某种奇怪的状态,从而导致进一步的问题。
| 归档时间: |
|
| 查看次数: |
692 次 |
| 最近记录: |