美文网首页iOS接下来要研究的知识点
collectionView reloadSections导致的

collectionView reloadSections导致的

作者: 言霏 | 来源:发表于2020-12-02 20:05 被阅读0次

    页面层级结构:

    VC.View -> scrollView -> HeaderView -> SliderView -> CollectionView -> Cell -> RankRadioIconView -> (label、image)

    Masnory警告:

    (
        "<MASLayoutConstraint:0x283e65980 RankRadioIconView:0x10aa0a100.left == UIView:0x10aa0a4a0.left>",
        "<MASLayoutConstraint:0x283e65b60 RankRadioIconView:0x10aa0a100.right == UIView:0x10aa0a4a0.right>",
        "<MASLayoutConstraint:0x283e65f20 SDAnimatedImageView:0x10aa05f10.left == RankRadioIconView:0x10aa0a100.left>",
        "<MASLayoutConstraint:0x283e65ce0 SDAnimatedImageView:0x10aa05f10.right == RankRadioIconView:0x10aa0a100.right>",
        "<MASLayoutConstraint:0x283e66520 UILabel:0x10aa0bfa0.left == SDAnimatedImageView:0x10aa05f10.left + 10>",
        "<MASLayoutConstraint:0x283e665e0 UILabel:0x10aa0bfa0.right <= SDAnimatedImageView:0x10aa05f10.right>",
        "<NSAutoresizingMaskLayoutConstraint:0x283952030 UIView:0x10aa0a4a0.width == 0>"
    )
    

    scrollView、HeaderView均使用frame约束,SliderView、CollectionView、Cell、RankRadioIconView、(label、image)都是用Masonry约束,UIView:0x10aa0a4a0.width == 0该view为Cell的contentView。起初怀疑是frame、Masonry一同使用姿势不对导致的,排查半天没发现异常使用。最终在看到:

    [self.collectionView reloadSections:[NSIndexSet indexSetWithIndex:0]];
    

    在VC的ViewDidLoad中用frame布局完scrollView、HeaderView后传入collectionView数据源调用了该行代码,换成[self.collectionView reloadData];后就没有警告了。

    思考

    reloadDatareloadSections的刷新方式肯定不同,在reloadData后的 collectionView:cellForItemAtIndexPath中cell.frame不为0,而reloadSections后cell.frame为0,导致Masonry告警。(cell采用注册方式创建[collectionView registerClass:[Cell class] forCellWithReuseIdentifier:@"CellID"];)

    相关文章

      网友评论

        本文标题:collectionView reloadSections导致的

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