UICollectionView 简单的使用和注意事项
2024-09-13 08:52:40
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;
}
最新文章
- HTML 学习笔记 JavaScript(call方法详解)
- RequireJs调研
- 【2016-10-13】【坚持学习】【Day4】【WPF】【ObservableCollection<;T>;】
- BootSrap学习
- [游戏模版6] Win32 graph
- 边工作边刷题:70天一遍leetcode: day 73
- LeetCode 423. Reconstruct Original Digits from English——学会观察,贪心思路
- 如何选择linux 版本
- Educational Codeforces Round 4 A. The Text Splitting 水题
- chrome浏览器默认样式覆盖input背景
- devi into python 笔记(二)元组 变量声明 和列表解析
- HDOJ/HDU 1088 Write a simple HTML Browser(HTML字符串)
- jsonp突破同源策略,实现跨域访问请求
- codeforces 600E. Lomsat gelral 启发式合并
- Android查缺补漏(View篇)--事件分发机制
- Docker实用技巧之更改软件包源提升构建速度
- spring @CrossOrigin解决跨域问题
- 利用表格分页显示数据的js组件bootstrap datatable的使用
- 科技论文之Latex公式&;amp;符号
- sql里 where和order by一起使用是怎样的顺序
热门文章
- UNIX网络编程---传输层:TCP、UDP、SCTP(二)
- ASP.NET Web API Authorization using Tokens
- sql2008R2数据库备份--双机备份
- hibernate 简单查询
- 查找EBS中各种文件版本(Finding File Versions in the Oracle Applications EBusiness Suite - Checking the $HEADER)
- notepad++中的zencoding的快捷键修改[转]
- xHTML+div布局:三个div,两边div宽度固定,中间div宽度自适应
- codeforces 10 D. LCIS LCIS O(n^2)算法
- GO语言搭建
- 图文:TF卡和SD卡的区别及什么是TF卡?什么是SD卡