多种Cell的CollectionView

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Ginhoor/article/details/41908589


制作拥有两个Cell的TableView还是比较容易的,创建一个拥有多种类型Cell的CollectionView还是需要一点设计的。


首先需要创建一个继承UICollectionViewFlowLayout的layout,用来指定不同的cell在CollectionView中如何正确的显示。


需要实现的方法:

  • (CGSize)collectionViewContentSize 用来指定CollectionView的显示范围
  • (UICollectionViewLayoutAttributes *)layoutAttributesForSupplementaryViewOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath 用来指定一个Hedaer的frame



  • (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath 设置单个Cell的UICollectionViewLayoutAttributes



  • (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect设置每个cell的LayoutAttributes数组




由于Layout没有DataSource,这里设计了一个协议让layout能了解到DataSource。


@protocol GHTreasureLayoutDataSource <NSObject>


// 指定每个Cell的CGRect

- (CGRect)treasureLayout:(GHTreasureLayout *)layout eachFrameForItemAtIndexPath:(NSIndexPath *)indexPath;


// 用于指定CollectionView的显示范围

- (CGSize)collectionViewContentSize:(GHTreasureLayout *)layout;


@end



这里只能简单的介绍了一下思路,具体的代码请下载Demo代码。

链接: http://pan.baidu.com/s/1mgDWc8w 密码: r2aa


猜你喜欢

转载自blog.csdn.net/Ginhoor/article/details/41908589
今日推荐