关于UICollectionView的headerView的问题

来源:互联网 发布:富木制衣淘宝旗舰店 编辑:程序博客网 时间:2024/06/05 15:11

1.collectionView的注意事项:必须注册cell;如果在storyboard添加了可重用标示符,可以不注册.

             必须实现代理方法.否则会报错.

             当cell的大小显示不正常的时候,可以试一下在视图加载完成之后,将要出现的时候,设置itemSize;  注册cell的方法会跳到dequeueReusableCellWithReuseIdentifier:(缓存池中调用cell),如果缓存池中没有会创建cell,这时候会自动调用 -(instancetype)initWithFrame:(CGRect)frame这个方法.

在storyboard 中设置cell的颜色不起作用,要在控制器中用代码设置.


2.collectionView纯代码设置组头:

-(CGSize)collectionView:(UICollectionView*)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section

{

    if(section == 0)

    {

        CGSize size = {320, 150};

        return size;

    }    else

    {

        CGSize size = {320, 50};

        return size;

    }


}


3.

#warning 如果在storboard中设置了组头或组尾,必须设置重用标识符

    

    //这个方法必须要在storyboard中设置可视化header,footer,可重用标示符,才会起作用

//    static NSString *headerIdentifier = @"header";

    UICollectionReusableView *resuableView;

//    headerIdentifier = (kind == UICollectionElementKindSectionHeader) ? @"header" : @"footer";

    if(kind ==UICollectionElementKindSectionHeader){

    resuableView = [collectionView dequeueReusableSupplementaryViewOfKind:kindwithReuseIdentifier:@"header"forIndexPath:indexPath];

//            return resuableView;

    }else{

        resuableView = [collectionView dequeueReusableSupplementaryViewOfKind:kindwithReuseIdentifier:@"footer"forIndexPath:indexPath];

//        return resuableView;


    }

    return resuableView;

    

}


4.    2的方法和3的方法同时使用会报错.


0 0
原创粉丝点击