UICollectionView的使用

1.传递布局
首页创建UICollectionViewController时,需要给它传一个展示的布局,一般九宫格用的都使流水布局!
    //1.创建流水布局
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    //2.设置每个格子的尺寸
    layout.itemSize = CGSizeMake(200, 200);
    //3.设置整个collectionView的内边距
    CGFloat paddingY = 20;
    CGFloat paddingX = 20;
    layout.sectionInset = UIEdgeInsetsMake(paddingY, paddingX, paddingY, paddingX);
    //4.设置每一行之间的间距
    layout.minimumLineSpacing = paddingY;


2.初始化UICollectionViewController,设置展示布局
//初始化 UICollectionViewController
UICollectionViewController *controller = [[UICollectionViewController alloc] initWithCollectionViewLayout:layout]

在对 UICollectionViewController 的 View 进行相关属性设置和属性修改时,记得要拿到UICollectionViewController.collectionView 再设置。

//设置背景

self.collectionView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@'bg_deal']];

3.3.显示数据                                           

记得分别实现

<UICollectionViewDelegate, UICollectionViewDataSource>

之后再调用代理数据源方法和代理方法

#pragma mark - 数据源方法
/**
 *  第section组有多少个格子(cell)
 */
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _deals.count;
}

//每个格子的内容
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
//    indexPath.item    某一组的哪一个
//    indexPath.section 哪一组
    // 1.创建cell
    QCDealCell *cell = [QCDealCell cellWithCollectionView:collectionView indexPath:indexPath];
    
    // 2.取出模型,传递模型
    cell.deal = _deals[indexPath.item];
    
    return cell;
}
转载自:http://www.it165.net/pro/html/201402/9178.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值