废话不多说,列几个列子 (几种情况下的做法):

情景一:

介绍:1. 在UIViewController 上加 UICollectionView (用代码 创建 UICollectionView)。

   2. UICollectionView上的cell为自定义的view,名字叫:MyDealCell,用的是 xib。 ( 新建类 MyDealCell 继承自 UICollectionViewCell )

   3. 选中 MyDealCell.xib 修改其 identifier 为 ListCell。

   4. 注册 cell 用 registerClass 。

     5. 在iPad上测试。

=======GoodListViewController.h=======

@interface GoodListViewController : UIViewController<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>

@end

=======GoodListViewController.m=======

static NSString * const reuseIdentifier = @"ListCell";

- (void)viewDidLoad {
    [super viewDidLoad];

UICollectionViewFlowLayout *flowLayout=[[UICollectionViewFlowLayout alloc] init];
    flowLayout.scrollDirection=UICollectionViewScrollDirectionVertical;
    collection=[[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:flowLayout];
    collection.dataSource=self;
    collection.delegate=self;
    collection.backgroundColor=[UIColor redColor];
    [self.view addSubview:collection];

// [self.collectionView registerNib:[UINib nibWithNibName:@"MyDealCell" bundle:nil] forCellWithReuseIdentifier:reuseIdentifier]; // MyDealCell用Xib实现的
    [collection registerClass:[GoodListCell class] forCellWithReuseIdentifier:reuseIdentifier]; // GoodListCell 里面的控件既可以用代码实现,也可以用xib,用xib的话,在 GoodListCell 里面要通过 GoodListCell *goodsView = [[NSBundle mainBundle] loadNibNamed:@"CollectionCell" owner:self options:nil][0]; 加载xib。
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 23;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{

  // 在 [collection dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];中的 collection 和 上面 形参 collectionView 一样
    MyDealCell *cell=[collection dequeueReusableCellWithReuseIdentifier:reuseIdentifier  forIndexPath:indexPath];
    cell.backgroundColor = [UIColor yellowColor];
    return cell;
}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(Screen_Width/3-10, Screen_Height-104);
}

-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(20, 7.5, 20, 7.5);
}

-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 7.5;
}

-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 20;
}

情景二:

介绍:1. GoodListViewController 继承自 UICollectionViewController

   2. UICollectionView上的cell为自定义的view,名字叫:MyDealCell,用的是 xib。( 新建类 MyDealCell 继承自 UICollectionViewCell )

   3. 选中 MyDealCell.xib 修改其 identifier 为 ListCell。

   . 注册cell用 registerNib

   5. 例子是在iPad上运行调试。

=======GoodListViewController.h=======

@interface GoodListViewController : UICollectionViewController

@end

static NSString * const reuseIdentifier = @"ListCell";

- (instancetype)init
{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    // cell的大小
    layout.itemSize = CGSizeMake(305, 305);
    return [self initWithCollectionViewLayout:layout];
}

- (void)viewDidLoad {
    [super viewDidLoad];

self.collectionView.backgroundColor = [UIColor redColor];
  
    // Register cell classes
    [self.collectionView registerNib:[UINib nibWithNibName:@"MTDealCell" bundle:nil] forCellWithReuseIdentifier:reuseIdentifier];
    self.collectionView.alwaysBounceVertical = YES;
    
 //   // 添加上拉加载
 //   [self.collectionView addFooterWithTarget:self action:@selector(loadMoreDeals)];
}

/**
 当屏幕旋转,控制器view的尺寸发生改变调用
 */
- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
{
    // 根据屏幕宽度决定列数
    int cols = (size.width == 1024) ? 3 : 2;
    
    // 根据列数计算内边距
    UICollectionViewFlowLayout *layout = (UICollectionViewFlowLayout *)self.collectionViewLayout;
    CGFloat inset = (size.width - cols * layout.itemSize.width) / (cols + 1);
    layout.sectionInset = UIEdgeInsetsMake(inset, inset, inset, inset);
    
    // 设置每一行之间的间距
    layout.minimumLineSpacing = 50;
}

#pragma mark <UICollectionViewDataSource>
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    // 计算一遍内边距
    [self viewWillTransitionToSize:CGSizeMake(collectionView.width, 0) withTransitionCoordinator:nil];
  
    return 30;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    MTDealCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
    
     GoodListCell *cell=[collection dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
       cell.backgroundColor = [UIColor yellowColor];
       return cell;
}

#pragma mark <UICollectionViewDelegate>
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
  NSLog(@"=====selected=indexPath.item", indexPath.item);
}

情景三:

介绍:1. ViewController 继承自 UIViewController

     2. 在ViewController 控制器对应的故事版中直接拉一个 UICollectionView 控件。然后连线到控制器。

   3. UICollectionView上的cell为自定义的view,名字叫:CollectionCell,用的是 xib。( 新建类 CollectionCell 继承自 UICollectionViewCell )

     4. 选中刚刚新建的xib,更改类名为 CollectionCell

   5. 控制器中注册cell用 registerClass

   6. 例子在iphone上运行。

   6. 这个和上两中情景不一样的地方是:在控制器中用 registerClass,加载 CollectionCell 对应的 xib 的时候,是在 CollectionCell.m 中通过:

       NSArray *arrayOfViews = [[NSBundle mainBundle] loadNibNamed:@"CollectionCell" owner:self options:nil];加载的。

     上面的情景一:在控制器中用 registerClass 的时候,cell里面的控件是用代码实现的;

   上面的情景二:在控制器中用 registerNib 的时候,   cell里面的控件是在xib中实现的。

  

一、自定义Cell

1、新建类CollectionCell继承自UICollectionViewCell

2、新建Xib,命名为CollectionCell.xib

  a.选中CollectionCell.xib删掉默认的View,从控件中拖一个Collection View Cell(图3)到画布中,设置大小为95*116

  b.选中刚刚新建的xib,更改类名为CollectionCell

  c.在CollectionCell.xib的CollectionCell中添加一个Label, 然后连线。

==========CollectionCell.m , 重写 initWithFrame 方法 =============

- (id) initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        // 初始化时加载collectionCell.xib文件
        NSArray *arrayOfViews = [[NSBundle mainBundle] loadNibNamed:@"CollectionCell" owner:self options:nil];
        
        // 如果路径不存在,return nil
        if (arrayOfViews.count < 1)
        {
            return nil;
        }
        // 如果xib中view不属于UICollectionViewCell类,return nil
        if (![[arrayOfViews objectAtIndex:0] isKindOfClass:[UICollectionViewCell class]])
        {
            return nil;
        }
        // 加载nib
        self = [arrayOfViews objectAtIndex:0];
    }
    return self;
}

二、定义UICollectionView;

1、拖动一个Collection View到指定 ViewController控制器 的View上

2、连线dataSource和delegate,并创建映射(连线),命名 (在控制器中用这个属性) 为CollectionView

3、选中CollectionView的标尺,将Cell Size的Width和Height改成与自定义的Cell一样的95*116,

4、选中CollectionView的属性,可以修改其属性,比如是垂直滑动,还是水平滑动,选择Vertical或Horizontal

5、选中CollectionViewCell,修改Class,继承自CollectionCell。

=======ViewController.h=======

#import <UIKit/UIKit.h>
@interface ViewController : UIViewController

@end

=======ViewController.m=======

#import "ViewController.h"
#import "CollectionCell.h"

@interface ViewController () <UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>

@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;  // 通过连线创建的属性

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor lightGrayColor];
    
    self.collectionView.backgroundColor = [UIColor grayColor];
    [self.collectionView registerClass:[CollectionCell class] forCellWithReuseIdentifier:@"CollectionCell"];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;

}

//每个section的item个数
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 40;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
//    CollectionCell *cell = (CollectionCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"CollectionCell" forIndexPath:indexPath];
//    
//    //图片名称
//    NSString *imageToLoad = [NSString stringWithFormat:@"%d.png", indexPath.row];
//    //加载图片
//    cell.imageView.image = [UIImage imageNamed:imageToLoad];
//    //设置label文字
//    cell.label.text = [NSString stringWithFormat:@"{%ld,%ld}",(long)indexPath.row,(long)indexPath.section];
    
   CollectionCell *cell = (CollectionCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"CollectionCell"forIndexPath:indexPath];
    
    return cell;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"===indexPath.item=%d", (int)indexPath.item);
}

iOS-UICollectionViewController 介绍的更多相关文章

  1. iOS框架介绍

    iOS框架介绍      Cocoa Touch   GameKit  实现对游戏中心的支持,让用户能够在线共享他们的游戏相关的信息  iOS设备之间蓝牙数据传输   从iOS7开始过期   局域网游 ...

  2. iOS CoreData 介绍和使用(以及一些注意事项)

    iOS CoreData介绍和使用(以及一些注意事项) 最近花了一点时间整理了一下CoreData,对于经常使用SQLite的我来说,用这个真的有点用不惯,个人觉得实在是没发现什么亮点,不喜勿喷啊.不 ...

  3. iOS CoreData介绍和使用(以及一些注意事项)

    iOS CoreData介绍和使用(以及一些注意事项) 最近花了一点时间整理了一下CoreData,对于经常使用SQLite的我来说,用这个真的有点用不惯,个人觉得实在是没发现什么亮点,不喜勿喷啊.不 ...

  4. MWeb for iOS 测试版介绍

    目前已开始第二次测试:MWeb for iOS 版本发布说明,更新至第二次测试版本  上图为 MWeb for iOS 的图标,再次感谢 @Producter http://weibo.com/u/ ...

  5. 一个iOS 框架介绍:MKNetworkKit

    http://blog.csdn.net/kmyhy/article/details/12276287 http://blog.csdn.net/mobailwang/article/details/ ...

  6. iOS:Masonry介绍与使用

    Masonry介绍与使用实践:快速上手Autolayout   frame----->autoresing------->autoLayout-------->sizeClasses ...

  7. iOS多线程介绍

    一.线程概述 有些程序是一条直线,起点到终点:有些程序是一个圆,不断循环,直到将它切断.直线的如简单的Hello World,运行打印完,它的生命周期便结束了,像昙花一现那样:圆如操作系统,一直运行直 ...

  8. iOS - UICollectionViewController

    前言 NS_CLASS_AVAILABLE_IOS(6_0) @interface UICollectionView : UIScrollView @available(iOS 6.0, *) pub ...

  9. IOS JavaScriptCore介绍

    本文主要转自:https://www.jianshu.com/p/cdaf9bc3d65d http://blog.csdn.net/u011993697/article/details/515772 ...

  10. IOS AppStore介绍图的尺寸大小(还有一些自己被拒的分享...)

    4s:640*960 5:640*1136 6:750*1334 6P:1242*2208 -------现在新版本的iTunes connect里只上传6P版本的大小就可以了,其他版本苹果会自动分辨 ...

随机推荐

  1. Intellij IDEA 配置jrebel热部署

    Jrebel 先介绍一下jrebel,jrebel是可以热部署项目的一个工具,更改代码自动部署并不需要重启项目(在spring中的controller中,增加.修改方法都是可以进行热部署而不需要重启的 ...

  2. DOS常见命令

    dir: 显示一个目录中的文件和子目录 md: 创建目录 rd: 删除目录 cd: 进入指定目录 cd..: 退回到上级目录 cd\: 退回到根目录 del: 删除文件 set: 显示.设置.删除cm ...

  3. 【工作中学习2】Map的使用及排序(第三个参数)

    项目进行中,使用到Map(std::map),Map要点整理如下: 1. Map,也叫关联数组,提供key/value(键/值对),key用来索引,value是被存储和检索的数据. 2. key值唯一 ...

  4. OMD开源监控软件

    参考 Best Monitoring Solution - OMD (Nagios + Check_MK) 官网 mathias-kettner.com OMD labs.consol.de Conf ...

  5. 建立本地yum源

    使用环境 服务器处于内网,需要更新 网络资源紧张,节约带宽 建立yum目录 mkdir -p /opt/opmgmt/yum rsync服务器列表 centos mirrors epel mirror ...

  6. oop编程思想

    oop的编程思想:抽象.封装.继承.多态. 1.抽象: 数据抽象:类描述的对象的属性或状态 行为抽象:类描述的对象的行为或功能 举例: 时钟:Class 数据:int Hour,Minute,Seco ...

  7. lrzsz的使用

    可以方便的在本地PC机和远程服务器之间传输文件. 1.下载 直接在centos上执行命令yum -y install lrzsz 2.上传文件 rz // 上传文件,执行命令rz,会跳出文件选择窗口, ...

  8. css3弹性盒子

    CSS3 弹性盒子(Flex Box) 弹性盒子是 CSS3 的一种新的布局模式. CSS3 弹性盒( Flexible Box 或 flexbox),是一种当页面需要适应不同的屏幕大小以及设备类型时 ...

  9. hdu-3449 Consumer---有依赖性质的背包

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3449 题目大意: fj打算去买一些东西,在那之前,他需要一些盒子去装他打算要买的不同的物品.每一个盒 ...

  10. vue中css动画原理

    显示原理: <transition name='fade'> <div v-if='show'>hello world</div> </transition& ...