UICollectionView 在创建的时候,要给它一个UICollectionViewFlowLayout (不然会崩溃),就像tableview一样,也要为它注册自定义的cell。

 UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
[flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
//cell间距
flowLayout.minimumInteritemSpacing = 10.0f;
//cell行距
flowLayout.minimumLineSpacing = 20.0f;
//1.添加FlowLayout
UICollectionView *collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 64, 320, [UIScreen mainScreen].bounds.size.height - 64) collectionViewLayout:flowLayout]; //2.为collection 注册cell(自定义的cell)
[collectionView registerNib:[UINib nibWithNibName:@"CarCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"CarCollectionViewCell"];

还要为它设置代理和实现代理方法:

collectionView.backgroundColor = [UIColor whiteColor];
self.collectionView = collectionView;
self.collectionView.delegate = self;
self.collectionView.dataSource = self;
[self.view addSubview:self.collectionView];

设置cell的大小:

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
CGSize size = CGSizeMake(, );
return size;
}

设置整体cell在collection中的上下左右边距:

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
return UIEdgeInsetsMake(30, 20, 0, 20);
}

在cellForItemAtIndexPath中使用自定义的cell,注意自定义cell的xib文件时,要加上identifier:

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

    CarCollectionViewCell *cell = (CarCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"CarCollectionViewCell" forIndexPath:indexPath];
...
...
...
return cell;
}

最新文章

  1. HTML 学习笔记 JavaScript(call方法详解)
  2. RequireJs调研
  3. 【2016-10-13】【坚持学习】【Day4】【WPF】【ObservableCollection<T>】
  4. BootSrap学习
  5. [游戏模版6] Win32 graph
  6. 边工作边刷题:70天一遍leetcode: day 73
  7. LeetCode 423. Reconstruct Original Digits from English——学会观察,贪心思路
  8. 如何选择linux 版本
  9. Educational Codeforces Round 4 A. The Text Splitting 水题
  10. chrome浏览器默认样式覆盖input背景
  11. devi into python 笔记(二)元组 变量声明 和列表解析
  12. HDOJ/HDU 1088 Write a simple HTML Browser(HTML字符串)
  13. jsonp突破同源策略,实现跨域访问请求
  14. codeforces 600E. Lomsat gelral 启发式合并
  15. Android查缺补漏(View篇)--事件分发机制
  16. Docker实用技巧之更改软件包源提升构建速度
  17. spring @CrossOrigin解决跨域问题
  18. 利用表格分页显示数据的js组件bootstrap datatable的使用
  19. 科技论文之Latex公式&符号
  20. sql里 where和order by一起使用是怎样的顺序

热门文章

  1. UNIX网络编程---传输层:TCP、UDP、SCTP(二)
  2. ASP.NET Web API Authorization using Tokens
  3. sql2008R2数据库备份--双机备份
  4. hibernate 简单查询
  5. 查找EBS中各种文件版本(Finding File Versions in the Oracle Applications EBusiness Suite - Checking the $HEADER)
  6. notepad++中的zencoding的快捷键修改[转]
  7. xHTML+div布局:三个div,两边div宽度固定,中间div宽度自适应
  8. codeforces 10 D. LCIS LCIS O(n^2)算法
  9. GO语言搭建
  10. 图文:TF卡和SD卡的区别及什么是TF卡?什么是SD卡