iOS-TableViewCell.xib创建多个样式的Cell

在我们复用cell时,如果每个cell的布局不同,如实现以下的布局:

我们接下来用Xib文件来进行实现

1.创建TableViewCell文件,在Xib文件中生成6个Cell,并设置每个Cell的identifier,为了方便起见,我这里将其设置为数字0~5

2.在ViewController.m中创建Cell

TableViewCell*cell=[tableView dequeueReusableCellWithIdentifier:(nonnull NSString *)];
if (cell==nil) {
 cell=[[[NSBundle mainBundle]loadNibNamed:@"TableViewCell" owner:nil options:nil]
objectAtIndex:(NSUInteger)];
}
        return cell;

这几行代码是正常复用cell的流程,可以看到,identifier后面接当前Cell的识别码,index后面要加当前Cell所在位置。

例如:第一个Cell就是

TableViewCell*cell=[tableView dequeueReusableCellWithIdentifier:@"0"];
        if (cell==nil) {
            cell=[[[NSBundle mainBundle]loadNibNamed:@"TableViewCell" owner:nil options:nil]
firstObject];
        }

第二个Cell是

 TableViewCell*cell=[tableView dequeueReusableCellWithIdentifier:@"1"];
        if (cell==nil) {
            cell=[[[NSBundle mainBundle]loadNibNamed:@"TableViewCell" owner:nil options:nil]
objectAtIndex:1];
        }

所以在上面加一个判断语句,确定当前Cell的信息

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)
indexPath{
    NSString*identifier=0;
    NSInteger index=0;
//判断在第几组
    if (indexPath.section==0) {
        switch (indexPath.row) {
            case 0:
                identifier=@"0";
                index=0;
                break;
            case 1:
                identifier=@"1";
                index=1;
                break;
            case 2:
                identifier=@"2";
                index=2;
               break;
                
            default:
                break;
    }
    }else{
        switch (indexPath.row) {
            case 0:
                identifier=@"3";
                index=3;
                break;
            case 1:
                identifier=@"4";
                 index=4;
                break;
            case 2:
                identifier=@"5";
                 index=5;
                break;
                
            default:
                break;
        }
    }
    
        TableViewCell*cell=[tableView dequeueReusableCellWithIdentifier:identifier];
        if (cell==nil) {
            cell=[[[NSBundle mainBundle]loadNibNamed:@"TableViewCell" owner:nil options:nil]
objectAtIndex:index];
        }
        return cell;
}

3.可以把创建Cell的过程,在Cell.m文件中进行,以免ViewController中代码过多

TableViewCell.h

+(instancetype)setupCellWith:(UITableView*)tableView AtIndexPath:(NSIndexPath *)indexPath;

TableViewCell.m

+(instancetype)setupCellWith:(UITableView*)tableView AtIndexPath:(NSIndexPath *)indexPath{
    NSString*identifier=0;
    NSInteger index=0;
    if (indexPath.section==0) {
        switch (indexPath.row) {
            case 0:
                identifier=@"0";
                index=0;
                break;
            case 1:
                identifier=@"1";
                index=1;
                break;
            case 2:
                identifier=@"2";
                index=2;
                break;
                
            default:
                break;
                
        }
    }else{
        switch (indexPath.row) {
            case 0:
                identifier=@"3";
                index=3;
                break;
            case 1:
                identifier=@"4";
                index=4;
                break;
            case 2:
                identifier=@"5";
                index=5;
                break;
                
            default:
                break;
                
        }
    }
    
    TableViewCell*cell=[tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell==nil) {
        cell=[[[NSBundle mainBundle]loadNibNamed:@"TableViewCell" owner:nil options:nil]
objectAtIndex:index];
    }
    return cell;
}

ViewController.m文件中

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)
indexPath{
    TableViewCell*cell=[TableViewCell setupCellWith:tableView AtIndexPath:indexPath];
    return cell;
   }

4.把控件拖到Cell中

 

 

这样我们的布局就完成了ಠ౪ಠ

 

转载于:https://my.oschina.net/sgcllr/blog/779062

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值