场景:在开发中,在storyboard中建立一个UIViewController,又在上面拖进去了UICollectionView控件,这时想要使用自定义的cell和sectionHeader 而且不想单首创建子类的.xib文件ide
在storyboard外建立单元格和section头视图的子类(.h和.m),在stroyboard对应的cell和section的归属设为上一步建立的,并设置复用的identifierspa
使用的时候,不用再次注册单元格和复用的头尾视图,只用在对应的代理方法直接寻找就行了代理
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {开发
NSString *sectionidentifier = NSStringFromClass([CollectionSectionCell class]);it
if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {io
CollectionSectionCell *section = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:sectionidentifier forIndexPath:indexPath];class
return section; //返回头视图queue
}方法
return nil;margin
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
NSString *cellidentifier = NSStringFromClass([FavoriteCell class]);
FavoriteCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellidentifier forIndexPath:indexPath];
return cell;
}