UI 18 UICollectionView
2015-08-29 16:02
316 查看
UICollectionView 是 tableView 的升级版!
#import "ViewController.h" #import "MyCell.h" #import "UIImageView+WebCache.h" #import "MyCollectionReusableView.h" @interface ViewController ()<UICollectionViewDataSource, UICollectionViewDelegate> @property(nonatomic, retain)NSMutableArray *arr; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. // UICollectionView 他是苹果官方提供的一种瀑布效果. // 每一个用来显示的item它的尺寸有多大. UICollectionViewFlowLayout *flowLayOut = [[UICollectionViewFlowLayout alloc] init]; flowLayOut.itemSize = CGSizeMake(118, 210); // 设置一下行间距 flowLayOut.minimumLineSpacing = 10; // 列间距 flowLayOut.minimumInteritemSpacing = 10; // // 设置默认滚动方向, 默认是垂直方向. // flowLayOut.scrollDirection = UICollectionViewScrollDirectionHorizontal; // 设定头或者尾视图尺寸 flowLayOut.headerReferenceSize = CGSizeMake(0, 80); UICollectionView *collection = [[UICollectionView alloc] initWithFrame:CGRectMake(0,64, 375, 667 - 64) collectionViewLayout:flowLayOut]; // 接下来就是和tableView很相似,使用前需要前两个协议. collection.delegate = self; collection.dataSource = self; [self.view addSubview:collection]; [collection release]; // 通过注册的方式,创建Cell. // 第一个参数: 需要制定注册对象的类型 // 第二个参数: 重用池的标志. [collection registerClass:[MyCell class] forCellWithReuseIdentifier:@"reuse"]; //注册一个头视图 // 第一个参数: // 第二个参数: 指定是头视图还是尾视图,常量字符串在系统的UICollectionViewFlowLayout类的最上面 // 第三个参数: 重用标志 [collection registerClass:[MyCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerView"]; [self createData]; } // 创建头视图 - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{ if ([kind isEqualToString:UICollectionElementKindSectionHeader]) { MyCollectionReusableView *view = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerView" forIndexPath:indexPath]; view.myLabel.text = @"哼哼"; view.myLabel.textColor = [UIColor cyanColor]; return view; }else { return nil; } } #warning 使用注册的方式创建的cell,必须使用自定义的cell,否则会在里面重复大量的创建视图(tableViewli的cell,在外面创建一个按钮加进去是应为按钮和Cell一同进入重用池),未来了杜绝重复创建,必须使用自定义cell. - (void)createData{ NSString *path = [[NSBundle mainBundle]pathForResource:@"Data" ofType:@"json"]; NSData *data = [NSData dataWithContentsOfFile:path]; NSMutableArray *Aarr = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil]; self.arr = [NSMutableArray array]; for (NSMutableDictionary *temp in Aarr) { [self.arr addObject:temp[@"thumbURL"]]; } } // 和TableView一样,有两个必须要实现的方法. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{ return self.arr.count; } - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{ #warning 在CollectionCell的创建时候,提供了另外一种不同于tableView的Cell创建方式. MyCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath]; // 只有通过注册的方式创建的cell,在取值的时候就不需要就不需要在进行是否为空的判断. cell.contentView.backgroundColor = [UIColor redColor]; NSString *temp = self.arr[indexPath.item]; [cell.imageView sd_setImageWithURL:[NSURL URLWithString:temp]]; cell.label.text =[NSString stringWithFormat:@"%ld",indexPath.row]; return cell; } - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{ }
相关文章推荐
- 实现自适应的UI界面
- UIPageControl, UIScrollView属性及UIScrollViewDelegate详解
- IOS UICollectionView
- 1041. Be Unique (20)
- UINavigationController及界面传值
- squeeze()
- ip_queue内核通信
- Storyboard中 UIViewController+UITableView使用 autolayout引起的界面布局问题
- UGUI研究院之全面理解图集与使用
- UI 16 数据持久化
- UI 15 UITableViewController & 系统自带快捷菜单 & 系统下拉刷新
- win10无法升级Win10 Build 10532?也许Hyper-V在搞鬼
- 【Android UI设计与开发】第05期:引导界面(五)实现应用程序只启动一次引导界面
- UI设计-不简单?简单(2)
- UI 15 NetWorkingTool 封装JSON解析
- uva 11538 - Chess Queen 先分步还是先分类? 计数问题
- 【Android UI设计与开发】第04期:引导界面(四)仿人人网V5.9.2最新版引导界面
- STL源码剖析 [容器](五)[stl_deque.h]
- 【Android UI设计与开发】第03期:引导界面(三)仿微信引导界面以及动画效果
- ORA-00001: unique constraint (...) violated并不一定是数据冲突