年年有"余"

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 5829|回复: 0

UICollectionView的使用(与UITableView的比较)

[复制链接]
  • TA的每日心情
    奋斗
    2022-12-13 21:26
  • 签到天数: 371 天

    [LV.9]以坛为家II

    发表于 2014-5-30 16:09:09 | 显示全部楼层 |阅读模式
    本帖最后由 Sian 于 2014-5-30 16:16 编辑

    1、先看两者比较的效果图

    160302_4zyd_865233.png     iOS 模拟器屏幕快照“2014年5月30日 下午3.20.46”.png

    2、特点:

    2.1、都继承自UIScrollView,因此这两者可以说是兄弟关系,都是用于数据展示方面都是非凡的表现,并且UICollectionView自iOS6以来才支持;

    2.2、Collection View的整体构成元素,共有三个要素,分别如下

    • Cells(单元格)
    • Supplementary Views(补充的view,相当于TableView的页眉和页脚)
    • Decoration Views(装饰View,用于装饰整个CollectionView的

    2.3、数据模型(数据提供者UICollectionViewDataSource),主要功能:

    • Section数目
    • Section里面有多少item
    • 提供Cell和supplementary view设置

    分别为:
    1. numberOfSectionsInCollectionView:
    2. collectionView:numberOfItemsInSection:
    3. collectionView:cellForItemAtIndexPath:
    复制代码
    2.4、Cell和View的重用
    在iOS6中,Cell重用改善,我们可以更加方便的使用Cell,系统总是为我们初始化Cell。我们可以直接使用。只需要简单的按照两步走即可:

    1)  必须使用下面的方法进行Cell类的注册:
    1. - (void)registerClass:forCellWithReuseIdentifier:
    2. - (void)registerClass:forSupplementaryViewOfKind:withReuseIdentifier:
    3. - (void)registerNib:forCellWithReuseIdentifier:
    4. - (void)registerNib:forSupplementaryViewOfKind:withReuseIdentifier:
    复制代码
    2)  从队列中取出一个Cell,具体方法如下:
    1. -(id)dequeueReusableCellWithReuseIdentifier:forIndexPath:
    2. -(id)dequeueReusableSupplementaryViewOfKind:withReuseIdentifier:forIndexPath:
    复制代码
    2.5、界面布局
    collectionView与tableView最大的不同点就在于此,collectionView必须要使用自己的layout(UICollectionViewLayout)
    如:
    1.     UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    2.     flowLayout.itemSize = CGSizeMake(52, 52);               // cell大小
    3.     flowLayout.minimumInteritemSpacing = 1;                 // cell间距
    4.     flowLayout.minimumLineSpacing = 1;                      // cell行距
    5.     flowLayout.sectionInset = (UIEdgeInsets){81,1,1,1};     // cell边距
    复制代码
    创建collectionView需要带Layout的初始化方法:
    1. - (id)initWithFrame:(CGRect)frame collectionViewLayout:(UICollectionViewLayout *)layout;
    复制代码
    3、示例
    1. //
    2. //  CarLicenseView.m
    3. //  RouteMapInformation
    4. //
    5. //  Created by yusian on 14-5-29.
    6. //  Copyright (c) 2014年 yusian. All rights reserved.
    7. //
    8. #import "CarLicenseView.h"
    9. /****************************Cell定义*********************************/
    10. @interface SALicenseCell : UICollectionViewCell
    11. @property (nonatomic, strong) UILabel *label;
    12. @end
    13. @implementation SALicenseCell
    14. - (id)initWithFrame:(CGRect)frame
    15. {
    16.     self = [super initWithFrame:frame];
    17.     if (self) {
    18.         
    19.         // 键盘文字设置
    20.         self.label = [[UILabel alloc] init];
    21.         self.label.backgroundColor = [UIColor clearColor];  // 透明颜色
    22.         self.label.textAlignment = NSTextAlignmentCenter;   // 文字居中
    23.         self.label.frame = CGRectMake(0, 0, 52, 52);        // 位置大小
    24.         self.label.font = [UIFont systemFontOfSize:20];     // 文字大小
    25.         self.backgroundColor = [UIColor whiteColor];        // 背景颜色
    26.         
    27.         
    28.         [self.contentView addSubview:self.label];
    29.     }
    30.     return self;
    31. }
    32. + (NSString *)ID
    33. {
    34.     return @"licenseCellID";
    35. }
    36. @end
    37. /****************************Cell定义*********************************/
    38. @interface CarLicenseView () <UICollectionViewDataSource, UICollectionViewDelegate, UIAlertViewDelegate>
    39. @end
    40. @implementation CarLicenseView
    41. #pragma mark 界面初始化
    42. - (id)initWithFrame:(CGRect)frame
    43. {
    44.     // 1、键盘按钮布局
    45.     UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    46.     flowLayout.itemSize = CGSizeMake(52, 52);               // 按键大小
    47.     flowLayout.minimumInteritemSpacing = 1;                 // 按键间距
    48.     flowLayout.minimumLineSpacing = 1;                      // 按键行距
    49.     flowLayout.sectionInset = (UIEdgeInsets){81,1,1,1};     // 按键边距
    50.    
    51.     // 2、键盘附加视图
    52.     self = [super initWithFrame:frame collectionViewLayout:flowLayout];
    53.     if (self) {
    54.         // 键盘背景色
    55.         self.backgroundColor = [UIColor colorWithWhite:0.9 alpha:0.8];
    56.         
    57.     }
    58.    
    59.     // 3、注册按钮类(内部处理机制)
    60.     [self registerClass:[SALicenseCell class] forCellWithReuseIdentifier:[SALicenseCell ID]];
    61.     self.delegate = self;
    62.     self.dataSource = self;
    63.    
    64.     return self;
    65. }
    66. #pragma mark 界面绘制方法
    67. #pragma mark 键盘按键数目
    68. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
    69. {
    70.     return 30;
    71. }
    72. #pragma mark 键盘按键文字
    73. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
    74. {
    75.     SALicenseCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:[SALicenseCell ID] forIndexPath:indexPath];
    76.     cell.backgroundColor = [UIColor blueColor];
    77.     return cell;
    78. }
    79. @end
    复制代码
    参考链接:http://www.howzhi.com/group/iosDevelop/discuss/10134

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    手机版|小黑屋|Archiver|iOS开发笔记 ( 湘ICP备14010846号 )

    GMT+8, 2024-5-3 12:30 , Processed in 0.049821 second(s), 24 queries .

    Powered by Discuz! X3.4

    Copyright © 2001-2021, Tencent Cloud.

    快速回复 返回顶部 返回列表