iOS- UICollectionView

创建UICollectionView的步骤:
1.使用系统的布局UICollectionViewFlowLayout
2.自定义Cell
// 苹果专门写了一个标准瀑布流结构的布局
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    // layout的使用
    // 1.设置每个cell的大小
    flowLayout.itemSize = CGSizeMake(50, 50);
    // 2.设置collectionView的滚动方向
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    // 3.设置item间隔大小……
    flowLayout.minimumInteritemSpacing = 20;
    flowLayout.minimumLineSpacing = 20;
    flowLayout.sectionInset = UIEdgeInsetsMake(30, 30, 30, 30);
    
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:flowLayout];
    [collectionView setDelegate:self];
    [collectionView setDataSource:self];
    [collectionView registerClass:[MyCell class] forCellWithReuseIdentifier:@"lalala"];
    [self.view addSubview:collectionView];
    [collectionView release];

实现collectionView的协议方法


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return [self.array count];
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    MyCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"lalala" forIndexPath:indexPath];
    [cell.contentView setBackgroundColor:[UIColor yellowColor]];
    [cell.myLabel setText:[self.array objectAtIndex:indexPath.item]];
    return cell;
}

与UITableView的区别:

1.必须提前注册一个cell

[collectionView registerClass:[MyCell class] forCellWithReuseIdentifier:@"lalala"];

2.UICollectionViewCell内部没有任何label或者imageView,需要自定一个Cell
3.用一个类专门给collectionView提供布局信息

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值