IOS自定义UICollectionViewCell(对比UITableViewCell)

本篇文章主要介绍了"IOS自定义UICollectionViewCell(对比UITableViewCell)",主要涉及到方面的内容,对于IOS开发感兴趣的同学可以参考一下: UICollectionView是iOS6新引进的API,用于展示集合视图,布局更加灵活,其用法类似于UITableView。而UICollectionView...

UICollectionView是iOS6新引进的API,用于展示集合视图,布局更加灵活,其用法类似于UITableView。而UICollectionView、UICollectionViewCell与UITableView、UITableViewCell在用法上有相似的也有不同的,下面是一些基本的使用方法: 
对于UITableView,仅需要UITableViewDataSource,UITableViewDelegate这两个协议,使用UICollectionView需要实现UICollectionViewDataSource, 
UICollectionViewDelegate,UICollectionViewDelegateFlowLayout这三个协议,这是因为UICollectionViewDelegateFlowLayout实际上是UICollectionViewDelegate的一个子协议,它继承了UICollectionViewDelegate,它的作用是提供一些定义UICollectionView布局模式的函数,一些常用函数如下:

//设定指定Cell的尺寸
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath;

//设定collectionView(指定区)的边距
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section;

//设定指定区内Cell的最小行距
-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section;

//设定指定区内Cell的最小间距
-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section;

接来下我们开始自定义一个UICollectionViewCell。首先创建一个单独的类DoctorItemViewCell继承自UICollectionViewCell。 
实例化一个UICollectionView时,为了能够使用cell,必须得使用下面的方法进行Cell类的注册:

- (void)registerClass: (__unsafe_unretained Class)forCellWithReuseIdentifier: (NSString *)forCellWithReuseIdentifier:

而对于tableView来说并不需要在实例化时使用方法对Cell类进行注册。对于这个注册函数需要注意一点:registerClass:的参数一定要是我们所使用的那个UICollectionViewCell类,否则就会报错!因此我们需要这么写:

//docCollectionView为自定义的UICollectionView的实例对象//DoctorItemViewCell为自定义的UICollectionViewCell类[docCollectionView registerClass:[DoctorItemViewCell class] forCellWithReuseIdentifier:@"cell"];

UICollectionView的UICollectionViewDataSource和UICollectionViewDelegate协议的方法作用与UITableView的两个协议方法作用类似,例如:

//定义展示的UICollectionViewCell的个数
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section

//定义展示的UITableViewCell的个数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

//每个UICollectionView展示的内容 
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath

//每个UITableView展示的内容
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

自定义UICollectionViewCell时,在

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

方法里我们仅需写入:

// DoctorItemViewCell为自定义的UICollectionViewCell类
DoctorItemViewCell *cell = (DoctorItemViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:indentifier forIndexPath:indexPath]

而不需要像UITableViewCell那样判断Cell是否已被创建,这是因为在自定义UICollectionViewCell时有一个极其重要的方法:

-(id)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        //initialize code...
    }
    returnself;
}

这个方法是在初始化自定义的cell时系统自动调用的,所以不需要判断cell是否已经被创建。而对于UITableViewCell来说初始化方法并不唯一,所以我们需要在指定它的初始化方法。 
此外还需注意的一点是,自定义的UICollectionViewCell没有UITableViewCell如下类似的方法

DoctorItemViewCell *cell = (DoctorItemViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:indentifier];

而是有多一个indexPath参数的方法:

DoctorItemViewCell *cell = (DoctorItemViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:indentifier forIndexPath:indexPath];

在自定义UICollectionViewCell时需要注意的是,在cell里添加的那些信息变量是需要写在.h文件中作为公共的变量,否则在UICollectionView.m文件的

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

方法中是无法将cell所需的数据传给cell并展示在视图中的,因为collectionViewCell默认的也是惟一的初始化方法就是initWithFrame。所以根本无法在初始化方法中传递参数。而tableView就不用这样做,这是因为对于UICollectionViewCell类来说,它本身是不包含像UITableViewCell所含的imageView和titleLabel这样的控件属性的,所以自定义UICollectionViewCell时,需要将Cell所需要的控件属性放在UICollectionViewCell.h文件中作为外部可访问的公共属性。

最后附上参考代码:

//DoctorItemViewCell.h文件#import <UIKit/UIKit.h>#import "DoctorCollectionViewController.h"@interfaceDoctorItemViewCell : UICollectionViewCell//自定义的控件属性@property (strong, nonatomic)UIImageView *imageView;
@property (strong, nonatomic)UILabel *nameLabel;

@end
//DoctorItemViewCell.m文件
-(id)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.imageView = [[UIImageView alloc]initWithFrame:CGRectMake(kInterval, kInterval, 85, 105)];
        self.imageView.image = [UIImage imageNamed:@"appCover"];

        self.nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(self.imageView.frame)+kInterval, 8, 70, 22)];
        self.nameLabel.font = [UIFont systemFontOfSize:21];

        self.sexLabel = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(self.nameLabel.frame)+kInterval, 11, 16, 16)];
        self.sexLabel.font = [UIFont systemFontOfSize:14];
    }
    returnself;
}

collectionViewCell的创建方法:

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    staticNSString *indentifier = @"cell";
    DoctorItemViewCell *cell = (DoctorItemViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:indentifier forIndexPath:indexPath];

    cell.layer.cornerRadius = 5;
    cell.backgroundColor = [UIColor whiteColor];
    return cell;
}
本文网址链接:http://www.codes51.com/article/detail_116861.html
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值