場景:在開發中,在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;
}