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
原创粉丝点击