正确使用自定义的Cell

    在项目中,很多时候需要我们自定义tableViewCell。就我目前所了解的,有三种方式:纯代码自定义、xib自定义、storyboard自定义。这三种方式所创建出来的cell,在使用上,有些细微的差别,要是不注意,就很可能弄错。平时我也没太注意其中的差异,今天静下心来,整理了一番。再次强调,本文介绍的是,自定义cell的使用方法!

--------------------------------华丽丽的分割线----------------------------

    步入正题之前,先来了解一下这两个初始化cell的方法:

- (id)dequeueReusableCellWithIdentifier:(NSString *)identifier; // Used by the delegate to acquire an already allocated cell, in lieu of allocating a new one.

(根据指定的标示符,返回一个已经创建好的Cell,而不是重新创建一个新的Cell)

常见写法:

    static NSString *identifier = @"tableViewCell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];

    if (!cell) {

        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];

    }

    cell.textLabel.text = @"令人抓狂的cell";

    return cell;


--------------------------------华丽丽的分割线---------------------------

- (id)dequeueReusableCellWithIdentifier:(NSString *)identifier forIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(6_0);// newer dequeue method guarantees a cell is returned and resized properly, assuming identifier is registered

使用指定的标示符返回可重用的Cell,但是,使用这个方法之前,必须根据指定的标识符,为cell注册一个nib或者一个class。或者直接在storyboard上关联将要使用的cell,否则程序崩溃!

常见写法:

   static NSString *identifier = @"tableViewCell";

    [_myTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:identifier]; //注册

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];

    cell.textLabel.text = @"令人抓狂的cell";

    return cell;


--------------------------------华丽丽的分割线---------------------------

步入正题:

一、纯代码定义的cell(只有.h和.m文件,没有相对应的xib文件)

    纯代码自定义的cell,无非是继承UITableViewCell类,然后在子类的.m文件里面重写初始化方法,在初始化方法里,自定义自己所需要的东西。然后在tableView的 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath代理方法里,这样使用:

    static NSString *identifier = @"AAA";

    AAACell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];

    if (!cell) {

        cell = [[AAACell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];

    }

    return cell;


   或者这样:

    static NSString *identifier = @"AAA";

   //前文提到,使用第二种初始化cell的方法之前,必须先注册!由于AAACell没有xib文件,不能注册为nib,只能注册为class

    [self.myTableView registerClass:[AAACell class] forCellReuseIdentifier:identifier];

    AAACell *cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];

    return cell;


二、使用xib来自定义cell(有相对应的.h和.m文件)

   使用xib来自定义cell的时候,通常会创建相对应的.h和.m文件来关联xib上面的各个控件。一般来说,我们所需的自定义内容都在xib上直接拖控件完成,.h和.m文件里,几乎没有代码。

    如何在tableView的代理方法里使用这个xib文件呢,有两种方法:

    1、这种方法直接根据xib文件的名字,取得该xib所代表的cell。这个方法,有明显的缺点,就是它没有复用标识符,因此对内存的花销较大,不推荐该方法。

    NSArray *xibs = [[NSBundle mainBundle] loadNibNamed:@"BBBCell" owner:nil     options:nil]; 

    BBBCell *cell = [xibs lastObject];

    return cell;


   2、将xib文件转换成UINib对象,然后把复用标识符与UINib对象关联起来。

    static NSString *identifier = @"BBB";

    UINib *cellNib = [UINib nibWithNibName:@"BBBCell" bundle:[NSBundle mainBundle]];

    [self.myTableView registerNib:cellNib forCellReuseIdentifier:identifier];

    BBBCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];

    return cell;


    3、 将xib文件转换成UINib对象,然后把复用标识符与 UINib对象关联起来。

   static NSString *identifier = @"BBB";

    UINib *cellNib = [UINib nibWithNibName:@"BBBCell" bundle:[NSBundle mainBundle]];

    [self.myTableView registerNib:cellNib forCellReuseIdentifier:identifier];

    BBBCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];

    return cell;

    
   细心的小伙伴可能发现,2和3中,使用了不同的初始化cell的方法。前文提到,第二种初始化方法是需要注册的,但为何第一种初始化方法,也需要注册呢?我是这样认为的,这两个初始化cell的方法,都需要我们提供一个复用标识符,而将 xib文件与复用标识符关联起来的方法,唯有将 xib文件先转换成一个 UINib对象,然后使用注册方法 - ( void)registerNib:( UINib *)nib forCellReuseIdentifier:( NSString *)identifier 将它们绑定起来。( 关于xib与UINib的关系,我是这样理解的:xib是一种文件,而UINib是OC对象,不能在代码中,直接使用xib,而是要将xib转换成UINib对象,若是理解有误,请轻拍)。

三、在storyboard上自定义cell(需要.h和.m文件,不需要单独的xib文件)

    在故事板上自定义一个cell,流程一般是这样的,先继承UITableViewCell类,创建出CCCCell.h和CCCCell.m文件,然后前往故事板,在控制器的View上面添加一个UITableView,接着在UITableView上面拖一个UITableViewCell,紧接着在UITableViewCell上面自定义我们的内容,最后将这个cell的类型改为我们自定义的类型CCCCell。


    前往tableView的代理方法- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

   static NSString *identifier = @"CCC";

    CCCCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];

    //CCCCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];

    return cell;

}

两种初始化cell的方法都可以,因为我们已经将自定义的CCCCell直接关联到storyboard的tableView了,所以不需要额外注册。

  细心的小伙伴可能还发现,无论是xib还是storyboard,我都没有在它们上面设置cell的复用标识符。关于复用标识符,你可以只在代码中设置,不在xib或者storyboard上设置;也可以既在代码中设置,也在xib或者storyboard中设置(设置的复用标识符必须一致)。可不可以只在xib或者storyboard中设置,不在代码里写呢?明确告诉你,不可以,因为初始化cell的两个方法,都需要我们提供复用标识符。


--------------------------------华丽丽的分割线---------------------------

  

参考文章:UITableView属性和方法




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值