为了实现一个九宫格的图片选择器,如下图
很简单的实现,使用的是collectionView放置图片,给了cell一个点击事件,用来删除当前的图片,然后后面的图片前移
添加的部分就不说了,与题目无关,看代码
看代码,很简单的实现,清晰明了,但是一旦进行删除,crash掉了,
报错如下:
Assertion failure in -[UICollectionView _endItemAnimationsWithInvalidationContext:tentativelyForReordering:animator:], /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit_Sim/UIKit-3600.7.47/UICollectionView.m:5781
2017-04-24 15:17:02.808 MutiPhotoPicker[8549:277952] Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of items in section 0. *** The number of items contained in an existing section after the update (7) must be equal to the number of items contained in that section before the update (7)***, plus or minus the number of items inserted or deleted from that section (0 inserted, 1 deleted) and plus or minus the number of items moved into or out of that section (0 moved in, 0 moved out).'
终点看加粗部分啊,意思是collection删除前后的数据源必须和item的个数相符合,(我的代码没有写错,数据源是dataSource没错!!)
当我注释掉 selectAssets.remove(at: (indexPath?.row)!)
之后一切就和平了,这回删除就没有问题了,最后没办法把这句放倒删除item之后去执行了,取巧了
原因未知,难道是collection View默认 了selectAssets 为数据源了?
如果大神知道问题,还请提点一下