CollectionView在使用moveItemAtIndexPath, deleteItemsAtIndexPaths, insertItemsAtIndexPaths时,
如果出现这种报错:
attempt to insert item into section , but there are only items in section after the update
或是:
Invalid update: invalid number of items in section .The number of items contained in an existing section after the update () must be equal to the number of items contained in that section before the update (), plus or minus the number of items inserted or deleted from that section ( inserted, deleted) and plus or minus the number of items moved into or out of that section ( moved in, moved out)
多半是由于没有先处理好数据源, 所以一定要先操作数据源:
如果使用deleteItemsAtIndexPaths, 先remove
如果使用insertItemsAtIndexPaths, 先add
如果使用moveItemAtIndexPath, 先add, 再remove
这些操作都有动画, 可以这样写:
self.cv.performBatchUpdates({
self.cv.moveItemAtIndexPath(indexPath, toIndexPath: NSIndexPath.init(forRow: (self.modMe.mods[0].mods.count-1), inSection: 0))
}) { (finish) in
if finish {
UIView.performWithoutAnimation({ //取消动画
self.cv.reloadSections(NSIndexSet.init(index: 0))//刷新数据, 必要
self.cv.reloadSections(NSIndexSet.init(index: indexPath.section))
})
}
}
网友评论