UICollectionView 不同的单元格宽度

UICollectionView Different cells widths

你好,我想做这样的设计,我有一个 collectionView 有不同的部分,第一个部分必须不同...我该怎么做?

我制作了不同类型的单元格,但它的宽度始终与故事板中指定的宽度相同...有什么方法可以设置单元格的宽度吗?

非常感谢。

您需要采用UICollectionViewDelegateFlowLayout协议并实现collectionView(_:layout:sizeForItemAtIndexPath:)方法。实现此方法是为了将单个项目的大小指定给默认在集合视图中进行布局的 UICollectionViewFlowLayout 对象。

这里是link到the relevant documentation

你必须使用UICollectionViewDelegateFlowLayout,具体是这个方法:

- (CGSize)collectionView:(UICollectionView *)collectionView
                  layout:(UICollectionViewLayout *)collectionViewLayout
  sizeForItemAtIndexPath:(NSIndexPath *)indexPath

只要 return 在您需要的部分或 indexPath 中有您想要的大小。