UICollectionView(网格视图)


tableView非常类似,不过多了一个布局类

需要遵守的2个协议

<UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>



//网格布局类

    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];

    //设置网格视频的滑动方向

    layout.scrollDirection = UICollectionViewScrollDirectionVertical;

    //最低水平和垂直方向的间距(默认10,也可以通过代理设置)

    layout.minimumInteritemSpacing = 50;

    layout.minimumLineSpacing = 50;


    //创建网格视图,并且设置布局对象

    UICollectionView *cv = [[UICollectionView alloc] initWithFrame:frame collectionViewLayout:layout];


    //collectionView中的cell和段头段尾view都需要通过注册方式

    //注册段头kind使用:UICollectionElementKindSectionHeader

//注册段尾kind使用:UICollectionElementKindSectionFooter



1,必须实现的2个代理


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section

{//设置显示多少个cell

    return 15;

}


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

{//设置每个cell的内容(必须提前注册)

    MyCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"qqq" forIndexPath:indexPath];

    

    //config cell

    

    return cell;

}


2,其它


- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView

{//设置分段数量

    return 1;

}


- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath

{//cell大小,每行展示多少个cell,都依赖于cell的大小

    return CGSizeMake(100, 170);

}


- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section

{//整个集合分段上左下右的边距

    return UIEdgeInsetsMake(0, 0, 0, 0);

}


- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section

{//最低水平方向间距-->layout.minimumInteritemSpacing = 40;

    return 40;

}


- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section

{//最低垂直方向间距-->layout.minimumLineSpacing = 40;

    return 40;

}


- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath

{//cell点击事件

    NSLog(@"%d==%d",indexPath.section,indexPath.row);

}


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

{//设置段头view大小

    return CGSizeMake(30, 30);

}


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

{//设置段尾view大小

    return CGSizeMake(30, 30);

}


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

{//设置段头段尾view(必须提前注册了段头或段尾才能使用,类似cell)

    MyView *mv = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"www" forIndexPath:indexPath];

    

    return mv;

}






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值