很少用到UIcollectionview,前几天用到了,开始以为跟UITableview差不多呢,后来发现自己too young too simple,其中一个问题给我造成了很大的困扰,该怎么给UIcollectionview添加一个header view 呢?
寻找返回headerview的代理方法(类似UITableview),么有,失败
百度搜索到了正确的姿势
1、注册headerview
[_myCollcetionView registerClass:[VIPMainHeaderView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerId"];
2、新建一个headview类继承自UICollectionReusableView
3、实现代理方法
self.headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:headerId forIndexPath:indexPath];
[self.headerView viewWithButtonImagesNormal:self.myHeaderViewImageArrayUnsign];
return self.headerView;
}```
tips:由于不常用CollectionView所以新建时也遇到了一些问题,开始使用UICollectionViewLayout这个类实例的对象来初始化collection view的但是一直出错,而且在UICollectionview的初始化方法中提示的参数也是这个类,后来发现还有一个相似的类UICollectionViewFlowLayout,最后百度才大概明白这2个类的区别,
UICollectionViewFlowLayout 是 UICollectionViewLayout 的子类,是系统提供一种流式布局样式 !UICollectionViewLayout 定义了通用的接口,是自定义Layout的抽象基类 !
所以初始化UIcollectionview必须得用UICollectionViewFlowLayout