iOS经典讲解之UICollectionView
来源:互联网 发布:mysql 返回不重复数据 编辑:程序博客网 时间:2024/05/22 03:35
//// RootViewController.m// LessonUICollectionView//#import "RootViewController.h"@interface RootViewController () <UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>//UICollectionViewDelegateFlowLayout该类遵守了UICollectionViewDelegate// 实际上UICollectionViewDelegateFlowLayout这个协议是UICollectionViewDelegate的子协议@end@implementation RootViewController- (void)viewDidLoad { [super viewDidLoad]; [self addSubViews]; }// 创建一个集合视图 collectionView- (void)addSubViews{ // 初始化UICollectionView需要一个独立的类UICollectionViewFlowLayout来设置其布局 //UICollectionViewLayout是一个抽象类 本身没有具体功能 其功能是由其子类UICollectionViewFlowLayout来实现的 // Item 布局(网格状 布局) UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init]; // 行边距(相对于 上下滑动) 如果左右滑动 就是列边距 layout.minimumLineSpacing = 30; // 列边距(相对于 上下滑动) 如果左右滑动 就是行边距 layout.minimumInteritemSpacing = 30; // 设置Item的宽高 layout.itemSize = CGSizeMake(150, 200); // 设置滑动方向(默认是上下滑动的) layout.scrollDirection = UICollectionViewScrollDirectionVertical; // 设置表头 宽度不起作用 layout.headerReferenceSize = CGSizeMake(0, 100); // 设置表尾 宽度不起作用 layout.footerReferenceSize = CGSizeMake(0, 100); // 设置内边距 layout.sectionInset = UIEdgeInsetsMake(20, 20, 20, 20); // 用上面的layout布局初始化集合视图 UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:[UIScreen mainScreen].bounds collectionViewLayout:layout]; //设置代理 collectionView.dataSource = self; collectionView.delegate = self; // collectionView默认是黑的 collectionView.backgroundColor = [UIColor whiteColor]; // 显示视图 [self.view addSubview:collectionView]; [collectionView release]; [layout release]; // 注册你要用的cell //Identifier 重用标示符一定要和下面的一致 //registerClass:<#(Class)#> cell的类 系统的或你自定义的cell所属类 [collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"MyCell"]; // 注册表头 [collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"MyHeaderView"]; // 注册表尾 [collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"MyFooterView"]; }#pragma mark ------代理方法------// 必须实现的两个方法 跟tableView一样// 返回每个分区的Item数- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{ return 5;}// 返回每个Item的方法- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{ // 返回每个Item的方法 // 这个方法里面包括了之前tableViewCell写的那一大堆 不需要再alloc创建,如果集合中有会取出复用如果没有他会自动创建不需要自己再创建 // 必须要注册cell UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MyCell" forIndexPath:indexPath]; // 系统没有像tableView一样 给咱们 提供布局方式 // 要使用UICollectionViewCell一般都是自定义再使用 跟tableView一样所有自定义控件都要加到contentView上 cell.contentView.backgroundColor = [UIColor redColor]; return cell; }// 返回分区数跟tableView一样 默认一个- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{ return 1;}// 设置表头 表尾 同一个代理方法来是实现-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{ // 有参数kind判断返回表头还是表尾 // 因为参数是字符串的 判断相同不能等号要用方法isEqualToString: if ([kind isEqualToString:UICollectionElementKindSectionHeader]) { // 返回表头 需要去复用集合中得到 UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"MyHeaderView" forIndexPath:indexPath]; // 加颜色 headerView.backgroundColor = [UIColor greenColor]; return headerView; } else { // 返回表尾 UICollectionReusableView *footerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"MyFooterView" forIndexPath:indexPath]; footerView.backgroundColor = [UIColor yellowColor]; return footerView; }}@end
0 0
- iOS经典讲解之UICollectionView
- iOS经典讲解之NSUserDefaults
- iOS经典讲解之妙用UITextView
- iOS经典讲解之NSNotification(通知)
- iOS经典讲解之清除缓存文件
- iOS经典讲解之Swift数据类型详解
- iOS经典讲解之Socket使用教程
- iOS经典讲解之设置颜色注意事项
- iOS经典讲解之Apple Pay开发
- iOS经典讲解之UILabel居上居下显示
- iOS经典讲解之开发者账号对比
- ios之基础 UICollectionView
- IOS开发之UICollectionView
- IOS开发之UICollectionView
- iOS 之UICollectionView 使用
- iOS之UIcollectionView
- iOS开发之UICollectionView
- iOS开发之UICollectionView
- 使用git命令上传新的项目到git私有仓库
- 零基础学python-18.9 序列映射函数:map
- 体验复杂度
- Condtion实现生产者消费者通信
- Corosync+Pacemaker+DRBD+MySQL 实现高可用MySQL集群
- iOS经典讲解之UICollectionView
- webpack+react问题汇总
- 关于 HDFS 的 file size 和 block size
- 第四周项目2—建立“单链表”算法库
- sql存储过程查询栏目下面的所有子栏目
- MYSQL用户和权限操作
- 横竖屏切换时禁止重新加载Activity
- 安卓圆形头像制作两种方法。
- C#例题3-1简单的程序