调整CollectionView的单元格大小

时间:2019-01-20 10:12:31

标签: swift xcode

我需要制作类似技能页面的内容,因此我使用了集合视图,但我需要根据用户增加的技能来调整销售宽度以调整大小。我只需要调整单元格大小

1 个答案:

答案 0 :(得分:0)

您可以实现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)
}