【问题标题】:why does the dynamic collection cells in collection view doesnt display as given size and changes after scrolling in swift 3为什么集合视图中的动态集合单元格不显示为给定大小并且在 swift 3 中滚动后会发生变化
【发布时间】:2017-08-21 08:33:24
【问题描述】:

我有一个集合视图。我需要在序列中显示不同宽度的单元格。前 4 个单元格按要求显示,但在连续第 5 个图像消失后,其他单元格显示但未以所需方式显示。但它没有显示在模拟器中。滚动收藏视图时视图会变形。请任何人在 swift 3 中帮助我。

【问题讨论】:

    标签: ios swift3 uicollectionview


    【解决方案1】:

    如果你想调整UICollectionViewCell的大小尝试实现这个方法,在cellForRowAt里面设置frame是错误的。

    func collectionView(_ collectionView: UICollectionView!, layout _: UICollectionViewLayout!, sizeForItemAtIndexPath indexPath: IndexPath!) -> CGSize {
        if indexPath.row%3 == 0 {
            return CGSize(width: 172, height: 120)
        } else {
            .....
        }//switch, if, whatever
    }
    

    【讨论】:

    • func collectionView(_ collectionView: UICollectionView!, layout _: UICollectionViewLayout!, sizeForItemAtIndexPath _: IndexPath!) -> CGSize { for index in 0...contents.count { if (index % 3) == 0 || (index % 3) == 1 { yourWidth = 172 yourHeight = 120 } else { yourWidth = 355 yourHeight = 120 } return CGSize(width: yourWidth, height: yourHeight) } 我这样做了,但它显示的所有尺寸都相同细胞。
    猜你喜欢
    • 2023-03-19
    • 2021-11-02
    • 1970-01-01
    • 2017-11-05
    • 1970-01-01
    • 1970-01-01
    • 2016-12-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多