storyboard UICollectionView

场景:在开发中,在storyboard中创建一个UIViewController,又在上面拖进去了UICollectionView控件,这时想要使用自定义的cell和sectionHeader 并且不想单独创建子类的.xib文件

 

在storyboard外创建单元格和section头视图的子类(.h和.m),在stroyboard对应的cell和section的归属设为上一步创建的,并设置复用的identifier

 

使用的时候,不用再次注册单元格和复用的头尾视图,只用在对应的代理方法直接寻找就好了

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {

    

    NSString *sectionidentifier = NSStringFromClass([CollectionSectionCell class]);

    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {

        

        CollectionSectionCell *section = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:sectionidentifier forIndexPath:indexPath];

        return section;    //返回头视图

    }

    

    return nil;

}

 

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

    NSString *cellidentifier = NSStringFromClass([FavoriteCell class]);

    FavoriteCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellidentifier forIndexPath:indexPath];

    return cell;

}

posted @ 2017-01-11 17:20  binbins  阅读(467)  评论(0编辑  收藏  举报