首页 > 解决方案 > 调整 CollectionView 的单元格大小

问题描述

我需要制作类似技能页面的东西,所以我使用了集合视图,但我需要根据用户添加的技能来调整销售的宽度。我只需要如何调整单元格的大小

标签: swiftxcode

解决方案


您可以实现UICollectionViewDelegateFlowLayout协议并使用该collectionView:layout:sizeForItemAtIndexPath:方法。

例子:

func collectionView(_ collectionView: UICollectionView,
                    layout collectionViewLayout: UICollectionViewLayout,
                    sizeForItemAt indexPath: IndexPath) -> CGSize {
    let cellData = self.data[indexPath.item]
    let skills = cellData.skills // Or fetch the skills from somewhere else

    let width = skills.count * 100
    let height = 100
    return CGSize(width: width, height: height)
}

推荐阅读