UICollectionView

来源:互联网 发布:2015nba体测数据 编辑:程序博客网 时间:2024/05/21 19:08

初始化UICollectionViewFlowLayout

    // 创建一个网状结构布局    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];    // 设置网状结构的具体属性:    // 最小行间距    layout.minimumLineSpacing = 20;    // 最小列边距    layout.minimumInteritemSpacing = 20;    // item的大小 系统会根据宽高自动生成几排或几列    layout.itemSize = CGSizeMake(140, 200);    // 表头size    layout.headerReferenceSize = CGSizeMake(100, 100);    // 表尾size    layout.footerReferenceSize = CGSizeMake(100, 100);    // 滚动方向 默认是竖着的    layout.scrollDirection = UICollectionViewScrollDirectionVertical;    // 内边距    layout.sectionInset = UIEdgeInsetsMake(30, 30, 30, 30);    // 设置item强制size    layout.estimatedItemSize = CGSizeMake(140, 200);

用初始化的layout去初始化collectionView

 // 创建collectionView    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:[UIScreen mainScreen].bounds collectionViewLayout:layout];    // 设置代理与数据源代理    collectionView.delegate = self;    collectionView.dataSource = self;    // 设置背景颜色(默认是黑的)    collectionView.backgroundColor = [UIColor whiteColor];    [self.view addSubview:collectionView];    [collectionView release];    [layout release];

注册

如果是系统的cell 或者 header 和 footer 那么就给系统类注册

如果是自定义的cell header footer 那么就给自定义类注册(自定义类继承于系统类)

// 注册要使用的cell    [collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"MyCell"];    // 注册表头    [collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"MyHeaderView"];    // 注册表尾    [collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"MyFooterView"];

协议方法

// 返回每个分区的items数- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{    return 10;}// 返回每个索引下 UICollectionViewCell的样式- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{    // 重用池中有可以用的 就拿去用(返回给你)    // 如果没有就创建一个    // 必须注册一下 要使用的标识符的cell 才能使用    // 系统才知道 要从重用池中 取出哪个类型的cell    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MyCell" forIndexPath:indexPath];    cell.backgroundColor = [UIColor redColor];    return cell;}// 设置表头和表尾- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{    // 判断返回表头类型 还是 表尾类型    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {        // 表头        // 去重用池中找 判断一下 有就用 没有就建        // UICollectionElementKindSectionHeader 表头的类型        // 可重用的View 分为2种 一种表头 一种表尾        UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"MyHeaderView" forIndexPath:indexPath];        // 设置颜色        headerView.backgroundColor = [UIColor cyanColor];        return headerView;    }else{        // 表尾        UICollectionReusableView *footerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"MyFooterView" forIndexPath:indexPath];        footerView.backgroundColor = [UIColor brownColor];        return footerView;    }}
0 0
原创粉丝点击