'NSInternalInconsistencyException', reason: 'UICollectionView received layout attributes for a ...

来源:互联网 发布:高洛峰php百度网盘 编辑:程序博客网 时间:2024/05/17 08:56

swift3.0自定义UICollectionViewLayout 当数据改变collectionView.reloadData()时出现如下错误:

'NSInternalInconsistencyException', reason: 'UICollectionView received layout attributes for a cell with an index path that does not exist: {length = 2, path = 10 - 0}'


经过不断调试,出现这种情况的原因是数据切换时数组长度变化,但是UICollectionViewLayout中定义的itemAttributes并没有清空。还是拿的reloadData()之前的数据


解决办法:在继承了UICollectionViewLayout的类中重写以下方法,将itemAttributes置为空


 overridefunc invalidateLayout() {

        super.invalidateLayout()

        itemAttributes =nil

    }



0 0
原创粉丝点击