iOS——UICollectionView
2016-06-01 20:43
387 查看
一、UICollectionView概述
继承与UIScrollView的一个视图,也称为集合视图。UICollectionView的实现跟tableView不一样的地方在于Item的布局稍微复杂一点,需要用UICollectionViewLayout类来描述视图的布局。我们在项目中常用的是系统提供的UICollectionViewLayout类,也可以自定义UICollectionViewLayout。
二、UICollectionViewLayout
设置每个item的大小@property (nonatomic) CGSize itemSize;
设置每个item的最小行间距(默认是10)
@property (nonatomic) CGFloat minimumLineSpacing;
设置每个itme的最小列间距(默认是10)
@property (nonatomic) CGFloat minimumInteritemSpacing;
头部引用的尺寸
@property (nonatomic) CGSize headerReferenceSize;
尾部引用的尺寸
@property (nonatomic) CGSize footerReferenceSize;
设置UICollectionView的滑动方向
@property (nonatomic) UICollectionViewScrollDirection scrollDirection; typedef NS_ENUM(NSInteger, UICollectionViewScrollDirection) { UICollectionViewScrollDirectionVertical, //垂直 UICollectionViewScrollDirectionHorizontal //水平 };
三、UICollectionView
1.属性
初始化-(instancetype)initWithFrame:(CGRect)frame collectionViewLayout:(UICollectionViewLayout *)layout;
注册cell
-(void)registerClass:(nullable Class)cellClass forCellWithReuseIdentifier:(NSString *)identifier; -(void)registerNib:(nullable UINib *)nib forCellWithReuseIdentifier:(NSString *)identifier; -(void)registerClass:(nullable Class)viewClass forSupplementaryViewOfKind:(NSString *)elementKind withReuseIdentifier:(NSString *)identifier; -(void)registerNib:(nullable UINib *)nib forSupplementaryViewOfKind:(NSString *)kind withReuseIdentifier:(NSString *)identifier;
重用cell
-(__kindof UICollectionViewCell *)dequeueReusableCellWithReuseIdentifier:(NSString *)identifier forIndexPath:(NSIndexPath *)indexPath; -(__kindof UICollectionReusableView *)dequeueReusableSupplementaryViewOfKind:(NSString *)elementKind withReuseIdentifier:(NSString *)identifier forIndexPath:(NSIndexPath *)indexPath;
刷新collection
-(void)reloadData;
2.代理方法(必须)
返回个数-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section;
返回cell
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath;
3.代理方法(可选)
点击方法-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath;
分区数
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView;
四、返回头部、尾部视图样式
UICollectionView不能像UITableView一样直接指定头部和尾部视 图,需要注册使⽤用,最⼤大的好处是添加了重⽤用机制。1.注册头部、尾部视图
[self.collectionView registerNib:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerView"]; [self.collectionView registerNib:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footView"];
2.返回头部、尾部视图的样式
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath { //需要判断是否返回头部视图还是尾部视图 if ([kind isEqualToString:UICollectionElementKindSectionHeader]) { //初始化头部视图 UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerView" forIndexPath:indexPath]; //设置头部样式 headerView.backgroundColor = [UIColor redColor]; //返回头部视图 return headerView; }else{ if ([kind isEqualToString:UICollectionElementKindSectionFooter]) { //初始化尾部视图 UICollectionReusableView *footView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footView" forIndexPath:indexPath]; //设置尾部样式 footView.backgroundColor = [UIColor yellowColor]; //返回尾部视图 return footView; } } }
相关文章推荐
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 不可修补的 iOS 漏洞可能导致 iPhone 4s 到 iPhone X 永久越狱
- iOS 12.4 系统遭黑客破解,漏洞危及数百万用户
- 每日安全资讯:NSO,一家专业入侵 iPhone 的神秘公司
- [转][源代码]Comex公布JailbreakMe 3.0源代码
- 讲解iOS开发中基本的定位功能实现
- iOS中定位当前位置坐标及转换为火星坐标的方法
- js判断客户端是iOS还是Android等移动终端的方法
- iOS应用开发中AFNetworking库的常用HTTP操作方法小结
- iOS应用中UISearchDisplayController搜索效果的用法
- IOS开发环境windows化攻略
- iOS应用中UITableView左滑自定义选项及批量删除的实现
- iOS中UIAlertView警告框组件的使用教程
- 浅析iOS应用开发中线程间的通信与线程安全问题
- 检测iOS设备是否越狱的方法
- .net平台推送ios消息的实现方法
- 探讨Android与iOS,我们将何去何从?
- Android、iOS和Windows Phone中的推送技术详解