UICollectionView deleteItemsAtIndexPaths 崩溃

问题描述: UICollectionview,调用删除方法:deleteItemsAtIndexPaths ,崩溃,报错:Assertion failure in -[UICollectionViewData validateLayoutInRect:]。  UICollectionView received layout attributes for a cell with an index path that does not exist: {length = 2, path = 0 - 0}

原因分析:删除方法 崩溃,都是因为数据源不同步造成的。

解决方案: 首先,查看 dataSource,在调用删除方法之前,是否删掉了对应的 数据; 其次,如果自定义了 UICollectionViewFlowLayout,则 layoutAttributesForElementsInRect 这个方法返回的 布局属性 数组,也需要同步 删除,注意:这里因为是返回的布局属性,需要把最后一个 indexPath 对应的 属性删掉。因为比如有 10 个元素,你删掉了 第5个,那么相当于只有 9 个元素需要布局,只需要将 第 10 个元素的布局属性删掉 即可。

你可能感兴趣的:(IOS)