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