设为首页 加入收藏

TOP

CollectionView就是这么简单!(一)
2017-10-13 10:28:43 】 浏览:1017
Tags:CollectionView 就是 这么 简单

UICollectionView 和 UICollectionViewController 类是iOS6 新引进的API,用于展示集合视图,布局更加灵活,可实现多列布局,用法类似于UITableView 和 UITableViewController 类。

使用UICollectionView 必须实现UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout这三个协议。

 

1.首先我们创建CollectionView(代码如下)

 

- (void)initCollectionView

{

// 我是用masnory进行屏幕适配的 这个坐标你也可以直接给出。

    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {

      make.top.equalTo(self.view.mas_bottom).offset(0);

        make.top.offset(0); //顶部间隔

        make.left.offset(0);//左边

        make.bottom.offset(-10);//底部

        make.right.offset(0);//右边

 // 

    }];

    self.collectionView.backgroundColor = [UIColor colorWithRed:241 green:241 blue:241 alpha:1];//背景色

    self.collectionView.contentInset = UIEdgeInsetsMake(10, 10, 10, 10);//插入内容的位置 与边缘

    

    self.collectionView.delegate = self;代理协议

    self.collectionView.dataSource = self;数据源协议

    [self.collectionView registerClass:[CollectionViewCell class] forCellWithReuseIdentifier:@"cell"];

    创建新的cell

}

2.创建collectionview 必须要添加 UICollectionViewFlowLayout

//使用懒加载的方法

- (UICollectionView *)collectionView

{

    if (!_collectionView)

    {

        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];

        flowLayout.itemSize = CGSizeMake((SCREEN_WIDTH - 50)/4, (SCREEN_WIDTH - 50)/4 + 20);

        flowLayout.minimumLineSpacing = 10; 每排的间隔

        flowLayout.minimumInteritemSpacing = 10; 每行的间隔

        

        self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:flowLayout];

        [self.view addSubview:self.collectionView];

    }

    return _collectionView;

}

3.接下来 我们需要新建一个类(CollectionviewCell)

 

cell.h 文件中呢  我们把需要展示的内容 都有属性展示出来

比如 展示一个图片 和文字

@property (nonatomic, strong) UIImageView *familyImageView;

@property (nonatomic, strong) UILabel *titleLabel;

所以  接下来在.m中写这2个控件(我的坐标都是masnory适配  也可以直接给出)

-(instancetype)initWithFrame:(CGRect)frame

{

         if (self = [super initWithFrame:frame])

    {

        self.familyImageView=[[UIImageView alloc] init];

         self.titleLabel = [[UILabel alloc] init];

        self.titleLabel.font = [UIFont systemFontOfSize:13];

        self.titleLabel.textAlignment=NSTextAlignmentCenter;

        [self.contentView addSubview:self.titleLabel];

        [self.contentView addSubview:self.familyImageView];

//make  是masnory的一个属性 用于定义坐标位置

        [self.familyImageView mas_makeConstraints:^(MASConstraintMaker *make)

         {

             make.top.offset = 10;

        

首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇iOS 开发 -----公司测试打包上传.. 下一篇精仿百思不得姐客户端应用iOS源码

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目