美文网首页
UICollectionReusableView 使用

UICollectionReusableView 使用

作者: 廖燊 | 来源:发表于2022-08-09 12:08 被阅读0次

    最近遇到一个很奇葩的bug,collection头部没有被释放,但是在12.0以下的机型会消失,在xr 手机 14.0以上的系统有时候回偶现出不来

    问题截图

    最后查明原因,是因为UICollectionReusableView 的代理问题

    - (CGSize)collectionView:(UICollectionView*)collectionViewlayout:(UICollectionViewLayout*)collectionViewLayoutreferenceSizeForHeaderInSection:(NSInteger)section

    这个方法的 size 中的高度,必须大于0才会调用

    - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath

    最终解决办法是在 init 里面给 height 设置高度为0.01 触发代理 然后在动态计算好的高度赋值给height 然后 reload

    这里需要注意是 判断高度变化,如果变化才刷新,否则会导致死循环 

    遇到的奇葩bug都记录一下,方便以后查阅

    相关文章

      网友评论

          本文标题:UICollectionReusableView 使用

          本文链接:https://www.haomeiwen.com/subject/muhsertx.html