iOS-CollectionView 基础
2016-05-20 17:23
453 查看
CollectionView基础使用方法:
我们将完成以下效果
主要步骤如下:
在延展中把 collectionView 作为属性
注册 item
实现UICollectionViewDataSource 协议中必须实现的方法
实现UICollectionViewDelegate 协议中的一些方法
我们将完成以下效果
主要步骤如下:
在延展中把 collectionView 作为属性
@interface ViewController ()<UICollectionViewDelegate,UICollectionViewDataSource> @property (nonatomic,strong) UICollectionView *collectionView; @end
//创建 layout(此处创建的是流水布局) UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init]; //行距 flowLayout.minimumLineSpacing = 10; //列距 flowLayout.minimumInteritemSpacing = 10; //设置每个 item 的大小 flowLayout.itemSize = CGSizeMake(([UIScreen mainScreen].bounds.size.width -40)/3, ([UIScreen mainScreen].bounds.size.height-80)/4); //设置 item 的上左下右的边距大小 flowLayout.sectionInset = UIEdgeInsetsMake(10, 10, 20, 10); //设置 UICollectionView 的滑动方向 flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
注册 item
//注册 item [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:CellIdentifier]; //注册头部区域 [self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:Header]; //注册尾部区域 [self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:Footer];
实现UICollectionViewDataSource 协议中必须实现的方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{ return 20; } // The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath: - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{ UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath]; cell.contentView.backgroundColor = [UIColor redColor]; return cell; }
实现UICollectionViewDelegate 协议中的一些方法
//该方法用于设置 collectionView 的 header 和 footer - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{ //设置头部或尾部 view if (kind == UICollectionElementKindSectionHeader) { UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:Header forIndexPath:indexPath]; headerView.backgroundColor = [UIColor orangeColor]; return headerView; } else{ UICollectionReusableView *footerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:Footer forIndexPath:indexPath]; footerView.backgroundColor = [UIColor yellowColor]; return footerView; } } //点击 cell 时调用响应的方法 - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{ NSLog(@"didselected = %lu",indexPath.row); }
相关文章推荐
- 峰回路转,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应用开发中线程间的通信与线程安全问题
- 检测iOS设备是否越狱的方法
- .net平台推送ios消息的实现方法
- 探讨Android与iOS,我们将何去何从?
- Android、iOS和Windows Phone中的推送技术详解
- iOS推送的那些事