UI 18 UICollectionView
来源:互联网 发布:移动端ui框架 知乎 编辑:程序博客网 时间:2024/06/05 01:09
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{}
0 0
- UI 18 UICollectionView
- UI - UICollectionView
- iOS UI 18 uicollectionview和自定义cell
- UI day 19 UICollectionView
- iOS开发UI篇 -- UICollectionView
- UI课程21 集合视图UICollectionView
- IOS开发UI篇--UICollectionView初步入门
- iOS开发-UI控件:UICollectionView基础
- iOS开发 UI UICollectionView 瀑布流
- iOS开发系列 ---- UI(UICollectionView的使用)
- UICollectionView
- UICollectionView
- UICollectionView
- UICollectionView
- UICollectionView
- UICollectionView
- UICollectionView
- UICollectionView
- ESXi物理机内的虚拟机文件如何导出?
- 初识Hadoop——Hadoop-1.2.1和Hadoop-2.7.1的配置以及常见问题分析
- 3分钟制作一个APP,六款国际热门互联网产品
- CvvImage CopyOf 问题!!
- 虚拟机类加载机制
- UI 18 UICollectionView
- python - os模块中比较有用的部分
- NOIP2011 铺地毯 题解
- ActionBar简单使用——添加搜索按钮;让不同的Activity上显示不同的ActionBar;添加Tab标签;实现DrawerLayout并用ActionBarDrawerToggle控制
- 23Merge k Sorted Lists
- JVM性能调优
- leetcode 221: Maximal Square
- 名字的漂亮度
- 教你如何迅速秒杀掉:99%的海量数据处理面试题