Xib使用之TableViewCell.xib中创建多个Cell

初次使用xib创建UITableviewCell的时候,我都是一个xib文件里,只创建一个Cell,在实际业务中,往往都是一个列表中需要用到多个不同的Cell样式,这就需要创建N个.h .m .xib文件。而且这些.m中的实现还差不多。
后来发现,一个.xib文件中可以创建多个Cell,如图:


多个Cell

这样感觉方便多了。


具体实现:

第一步创建

先和普通创建xibCell一样,在xib中选中左边那个Cell,copy(command + c),然后paste(command + v).xib中就多个Cell了,O(∩_∩)O~~


多个Cell
第二步设置Identifier和代码使用

在代码中创建Cell时

   if (!cell) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"TempTableViewCell" owner:self options:nil] firstObject];
    }

TempTableViewCell是你的xib文件名,firstObject是第一个Cell,按顺序排的。
第二个怎么办??

        cell = [[[NSBundle mainBundle] loadNibNamed:@"TempTableViewCell" owner:self options:nil] objectAtIndex:2];

再多依次类推哈。(提示:如果在Cell中添加手势的话,loadNibNamed: 这个返回的数组中会比Cell多哦,大家注意)

设置每个Cell的identifier,(identifier 随意起的,我的规律就是类名+第几,不要重复就行)如图:


设置每个Cell的identifier


这样在重用队列中重复使用Cell的时候,能找到正确的Cell,

 TempTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TempTableViewCellFirst"];

可以根据indexPath设置不同的identifier。
可以把创建Cell的过程放在Cell.m中,做成类方法,这样不至于VC中的代码过多。
cell.h中:

@interface TempTableViewCell : UITableViewCell

/**
 *  @author god~long, 16-04-03 15:04:19
 *
 *  初始化Cell的方法
 *
 *  @param tableView 对应的TableView
 *  @param indexPath 对应的indexPath
 *
 *  @return TempTableViewCell
 */
+ (instancetype)tempTableViewCellWith:(UITableView *)tableView
                            indexPath:(NSIndexPath *)indexPath;


@end

cell.m中:

+ (instancetype)tempTableViewCellWith:(UITableView *)tableView indexPath:(NSIndexPath *)indexPath {
    NSString *identifier = @"";//对应xib中设置的identifier
    NSInteger index = 0; //xib中第几个Cell
    switch (indexPath.row) {
        case 0:
            identifier = @"TempTableViewCellFirst";
            index = 0;
            break;
        case 1:
            identifier = @"TempTableViewCellSecond";
            index = 1;
            break;
        case 2:
            identifier = @"TempTableViewCellThird";
            index = 2;
            break;

        default:
            break;
    }
    TempTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"TempTableViewCell" owner:self options:nil] objectAtIndex:index];
    }
    return cell;

}

这样VC中:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    TempTableViewCell *cell = [TempTableViewCell tempTableViewCellWith:tableView indexPath:indexPath];
    return cell;
}

是不是很方便呢。

设置属性

快捷设置xib属性:

1. 在.h或者.m中先写好属性,如图:

设置属性
2. 在xib中就可以拖拽连接属性了,如图:

property.gif

重点:关联属性的时候,你想关联那个Cell上的属性,需要先点击左边Cell列表,选中该Cell,然后再拖线关联上面的控件。
设置好属性,下面就是使用了,
配置Cell

/**
 *  @author god~long, 16-04-03 16:04:04
 *
 *  配置TempCell的方法
 *
 *  @param indexPath 对应TableView的indexPath
 */
- (void)configTempCellWith:(NSIndexPath *)indexPath;

.m中:

- (void)configTempCellWith:(NSIndexPath *)indexPath {
    switch (indexPath.row) {
        case 0: {
            _label1.text = @"我是Label1";
            _customImageView.image = [UIImage imageNamed:@"8"];
            break;
        }
        case 1: {
            _label2.text = @"我是Label2";
            [_customButton setTitle:@"我是button" forState:UIControlStateNormal];
            break;
        }
        case 2: {
            _label1.text = @"我是第三个Cell的Label1";
            _label2.text = @"我是第三个Cell的Label2";
            break;
        }
        default:
            break;
    }
}

重点:每个Cell设置链接的属性都是单独处理的,没连,在用的时候即使你用了,也设置不了。
运行效果:


运行效果

dome地址:点我点我



文/god_long(简书作者)
原文链接:http://www.jianshu.com/p/332e1db6ebb5
著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”。
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值