1。相关方法
- (void)registerNib:(UINib *)nib forSupplementaryViewOfKind:(NSString *)kind withReuseIdentifier:(NSString *)identifier //注册xib,以便后续重用
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath //具体配置内容 --- <UICollectionViewDataSource>
- (id)dequeueReusableSupplementaryViewOfKind:(NSString*)elementKind withReuseIdentifier:(NSString *)identifier forIndexPath:(NSIndexPath*)indexPath //重用
e.g. 自定义HeaderView、FooterView
创建xib,继承UICollectionReusableView
@interface FooterView : UICollectionReusableView
@property (weak, nonatomic) IBOutlet 。。。; //各控件声明
@end
============
- (UICollectionViewFlowLayout *) flowLayout{
UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
flowLayout.。。。。。。//各属性设置
flowLayout.headerReferenceSize = CGSizeMake(300.0f, 50.0f); //设置head大小
flowLayout.footerReferenceSize = CGSizeMake(300.0f, 50.0f);
return flowLayout;
}
UICollectionViewController *viewController = [[UICollectionViewController alloc] initWithCollectionViewLayout:[self flowLayout]];
================
static NSString *kCollectionViewCellIdentifier = @"CellId";
static NSString *kCollectionViewHeaderIdentifier = @"HeaderId";
static NSString *kCollectionViewFooterIdentifier = @"FooterId";
- (instancetype) initWithCollectionViewLayout:(UICollectionViewLayout *)layout{
self = [super initWithCollectionViewLayout:layout];
if (self != nil){
UINib *nib = [UINib nibWithNibName: NSStringFromClass([MyCollectionViewCell class]) bundle:[NSBundle mainBundle]];
[self.collectionView registerNib:nib forCellWithReuseIdentifier:kCollectionViewCellIdentifier]; //注册加载单元格
UINib *headerNib = [UINib nibWithNibName:NSStringFromClass([HeaderView class]) bundle:[NSBundle mainBundle]];
[self.collectionView registerNib:headerNib forSupplementaryViewOfKind :UICollectionElementKindSectionHeader withReuseIdentifier: kCollectionViewHeaderIdentifier ]; //注册加载头
UINib *footerNib = [UINib nibWithNibName:NSStringFromClass([FooterView class]) bundle:[NSBundle mainBundle]];
[self.collectionView registerNib:footerNib forSupplementaryViewOfKind: UICollectionElementKindSectionFooter withReuseIdentifier: kCollectionViewFooterIdentifier ];
}
return self;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
MyCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kCollectionViewCellIdentifier forIndexPath:indexPath];
cell. 。。。。。
return cell;
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
NSString *reuseIdentifier = kCollectionViewHeaderIdentifier;
if ([kind isEqualToString: UICollectionElementKindSectionFooter ]){
reuseIdentifier = kCollectionViewFooterIdentifier;
}
UICollectionReusableView *view = [collectionView dequeueReusableSupplementaryViewOfKind :kind withReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
if ([kind isEqualToString:UICollectionElementKindSectionHeader]){
HeaderView *header = (HeaderView *)view;
header.label.text = [NSString stringWithFormat:@"Section Header %lu", (unsigned long)indexPath.section + 1];
}
else if ([kind isEqualToString:UICollectionElementKindSectionFooter]){
FooterView *footer = (FooterView *)view;
NSString *title = [NSString stringWithFormat:@"Section Footer %lu", (unsigned long)indexPath.section + 1];
[footer.button setTitle:title forState:UIControlStateNormal];
}
return view;
}