UI总结 - CollectionView

collectionView经常用在商品展示(淘宝),图片展示等APP上,作用和用法和tableView差不多,最大的不同点是:collectionView里面有个瀑布流的用法,它可以在竖直滑动的情况下一横排不知可以铺一个cell,它可以铺很多cell.我觉得以后在APP项目中会出现越来越多的collectionView.上代码:
#import "ViewController.h"
#import "MyCell.h"
#import "UIImageView+WebCache.h"
@interface ViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property(nonatomic, strong)NSMutableArray *arr;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    //这是苹果给我们提供一种瀑布流
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    //layout设置item的尺寸
    layout.itemSize = CGSizeMake(120, 160);
    //最小的行间距
    layout.minimumLineSpacing = 30;
    //最小的列间距
    layout.minimumInteritemSpacing = 10;
    //修改滚动的方向,默认是垂直的方向
    //layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    //距离屏幕四边的距离
    layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    //layout.headerReferenceSize = CGSizeMake(200, 200);
    UICollectionView *collection = [[UICollectionView alloc]initWithFrame:self.view.frame collectionViewLayout:layout
                                    ];
    collection.delegate = self;
    collection.dataSource = self;
    collection.backgroundColor = [UIColor orangeColor];
    [self.view addSubview:collection];
    //注册
    [collection registerClass:[MyCell class] forCellWithReuseIdentifier:@"cell"];
    [self creatData];
}
-(void)creatData{
    NSString *path = [[NSBundle mainBundle]pathForResource:@"Data" ofType:@"json"];
    NSData *data = [NSData dataWithContentsOfFile:path];
    self.arr = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.arr.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    MyCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath] ;
    cell.contentView.backgroundColor = [UIColor cyanColor];
    cell.nameLable.text = [NSString stringWithFormat:@"%ld",indexPath.row];
    [cell.picImage sd_setImageWithURL:[NSURL URLWithString:self.arr[indexPath.row][@"thumbURL"]]];
    return cell;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"%ld",indexPath.row);
}
下面是cell文件:
//cell.h文件
#import <UIKit/UIKit.h>

@interface MyCell : UICollectionViewCell
@property(nonatomic, retain)UILabel *nameLable;
@property(nonatomic, retain)UIImageView *picImage;
@end

//cell.m文件
#import "MyCell.h"

@implementation MyCell
-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self creatView];
    }
    return self;
}

-(void)creatView{
    self.picImage = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.contentView.frame.size.width, self.contentView.frame.size.height - 50)];
    [self.contentView addSubview:self.picImage];
    
    self.nameLable = [[UILabel alloc]initWithFrame:CGRectMake(0, self.contentView.frame.size.height - 50, self.contentView.frame.size.width, 50)];
    [self.contentView addSubview:self.nameLable];
   
}


@end
2.gif
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,206评论 4 61
  • 上一篇讲了path的基本用法,这篇我们要去实现水波纹以及粘性小球这里就不得不提到贝塞尔曲线 一阶贝塞尔曲线: An...
    apkcore阅读 1,174评论 0 0
  • 昨天为了不让女儿吃过多的巧克力,开封之前和她达成协议:给爸爸留一块在冰箱、妈妈吃一块、朵朵一块。女儿欣然同意,...
    ovna阅读 861评论 1 7
  • 这世上坏人和好人一样多 我不敢打开心扉 因为我不敢确定你面具下的那张脸在我痛哭的时候是哭还是笑 ​​​
    陈希成菇凉阅读 93评论 0 0
  • 定义 委托是一个类,它定义了方法的类型,使得可以将方法当作另一个方法的参数来进行传递,这种将方法动态地赋给参数的做...
    困卡阅读 581评论 0 0