UICollectionView: 1>UICollectionViewFlowLayout流式布局

本文介绍了在XCode 7.2环境下,针对iOS 9.2开发目标,使用UICollectionViewFlowLayout创建流式布局的步骤,包括数据源准备、设置Section和Item数量、自定义CollectionCell、Header和Footer的设置,展示了从左到右、从上到下的布局效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

环境

XCode:Ver 7.2

DevelopeTarget: 9.2

UICollectionViewFlowlayout CollectionView流式布局

========================================================================================================

UICollectionView 使用时需要设置的点:

0> 数据源准备
- (void)viewDidLoad {
    [super viewDidLoad];

    _headerTitleDataSource = @[@"Section 1",@"Section 2",@"Section 3"];
    _collectionCellTitleDataSource = @[@[@"1-1",@"1-2",@"1-3"],
                                       @[@"2-1",@"2-2",@"2-3",@"2-4"],
                                       @[@"3-1",@"3-2",@"3-3"]];

    [self.testCollectionView setCollectionViewLayout:self.flowLayout animated:NO];
}

- (UICollectionViewFlowLayout *)flowLayout
{
    if (!_flowLayout) {
        _flowLayout = [[UICollectionViewFlowLayout alloc] init];
        [_flowLayout setItemSize:CGSizeMake(200, 100)];// 设置每个cell的大小
        [_flowLayout setMinimumInteritemSpacing:100.0f];// 水平方向相邻两个cell的最小距离
        [_flowLayout setMinimumLineSpacing:50.0f]; // 垂直方向相邻两个cell的最小距离
        [_flowLayout setHeaderReferenceSize:CGSizeMake(50, 100)];//设置高度。ps:宽度无论如何设置都是CollectionView的宽度,原因目前不明。
    }
    return _flowLayout;
}
1> 设置Section 个数
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
        return [_headerTitleDataSource count];
}
2> 设置每个Section中Item个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:  (NSInteger)section
{
    return [_collectionCellTitleDataSource[section] count];
}
3> 设置每个CollectionCell
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath;
{
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"collectionCell" forIndexPath:indexPath];
    UILabel *textLabel = [cell viewWithTag:1000];
    textLabel.text = _collectionCellTitleDataSource[indexPath.section][indexPath.row];

    //设置border容易看清楚每个cell
    textLabel.layer.borderColor = [UIColor blackColor].CGColor;
    textLabel.layer.borderWidth = 1.0f;
    textLabel.layer.cornerRadius = 3.0f;

    return cell;
}
4> 设置 HeaderView and FooterView
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
   UICollectionReusableView *reusableView = nil;
    if (kind == UICollectionElementKindSectionHeader) {
        reusableView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"reuseableHeaderView" forIndexPath:indexPath];
        reusableView.backgroundColor = [UIColor greenColor];
        UILabel *titleLabel = [reusableView viewWithTag:2000];
        titleLabel.text = _headerTitleDataSource[indexPath.section];
    }
    return reusableView;
}

效果图:

描述神马?

上面就是CollectionView的比较常见的流式布局。从左到右,从上到下挨个排,地方不够就换行。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值