重写UICollectionViewFlowLayout报cache mismatched frame警告

在重写UICollectionViewFlowLayout的时候会有很多坑,比如:

Logging only once for UICollectionViewFlowLayout cache mismatched frame
UICollectionViewFlowLayout has cached frame mismatch for index path <NSIndexPath: 0xc000000000200016> {length = 2, path = 0 - 1} - cached value: {{85, 40}, {55, 30}}; expected value: {{89, 40}, {55, 30}}
This is likely occurring because the flow layout subclass RHCollectionViewFlowLayout is modifying attributes returned by UICollectionViewFlowLayout without copying them

这个警告的大致原因是:返回的数组时,没有使用该数组的拷贝对象,而是直接使用了该数组。找了一圈资料,大部分是说需要进行深拷贝。
emmm...该方案是可行的,只是大部分找错了地方。

stackoverflow解决方案中找到了匹配的解决方法:
- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath方法中
将:UICollectionViewLayoutAttributes * currentItemAttributes = [super layoutAttributesForItemAtIndexPath:indexPath];
改成:UICollectionViewLayoutAttributes * currentItemAttributes = [[super layoutAttributesForItemAtIndexPath:indexPath] copy];

stackoverflow解决方式

具体GitHub解决案例

posted @ 2018-09-13 16:59  光是光光的光呐  阅读(1271)  评论(0编辑  收藏  举报