iOS-CollectionView 基础
来源:互联网 发布:linux server 版本官网 编辑:程序博客网 时间:2024/05/22 02:28
CollectionView基础使用方法:
我们将完成以下效果
主要步骤如下:
在延展中把 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);}
0 0
- iOS-CollectionView 基础
- iOS CollectionView
- iOS---collectionView
- ios collectionView
- CollectionView 基础(一)创建系统collectionview
- iOS collectionView使用
- iOS collectionview小结
- iOS开发中的collectionView
- iOS CollectionView简单使用
- collectionView的使用iOS
- iOS collectionView的使用
- iOS-CollectionView流水布局
- IOS CollectionView 线性布局
- iOS--集合视图CollectionView
- iOS 单元格CollectionView简介
- IOS开发之CollectionView
- iOS collectionView返回顶部
- iOS collectionView相册滑动
- Citrix XenServer体系架构解析
- PowerPoint轻松实现列表播放功能
- [Android]实现静默安装APK的两种方法
- SQL Server2008函数大全
- 3021
- iOS-CollectionView 基础
- python paramiko模块实现sftp上传下载文件
- Excel ActiveX教程(一)
- 单例设计模式
- CBitmap和HBITMAP的区别及相互转换方法
- key形式哈希表的线性探测
- MySQL之事务、索引、锁
- ios掩藏导航栏下面的线
- [Scikit-Learn] - 数据预处理 - 归一化/标准化/正则化