如何实现三种cell的tableview

需求如下:


屏幕快照 2018-09-17 下午5.35.56.png

针对这种服务端返回数据中是否包含预约时间、提报业主来确定样式的需求,项目中有好多种。现提供一个解决方案。当然这肯定不是最好的。可以通过如下三种方式实现哦。

  • 上一篇文章中具体如下 三种cell的实现方式
    是通过类族的方式将将子类的实现细节隐藏在抽象基类。
  • 而本章内容是通过不同的返回参数来判断使用当前哪一个cell。
  • 当然还可以根据一个cell就行了,根据类型改变布局。当然如果涉及到内容改变较多,人员调整,这种写法并不建议,可读性太差,后期维护成本比较多。所谓可读性是针对业务不了解的童鞋而言。

项目结构如下:

1537177836857.jpg

创建UITabelView
[_tableView registerClass:[SCOrderTodoDesListCell class] forCellReuseIdentifier:@"SCOrderTodoDesListCell"];
[_tableView registerClass:[SCOrderTodoSubDesCell class] forCellReuseIdentifier:@"SCOrderTodoSubDesCell"];
[_tableView registerClass:[SCOrderTodoListCell class] forCellReuseIdentifier:@"SCOrderTodoListCell"];

-(UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
        [_tableView registerClass:[SCOrderTodoDesListCell class] forCellReuseIdentifier:@"SCOrderTodoDesListCell"];
        [_tableView registerClass:[SCOrderTodoSubDesCell class] forCellReuseIdentifier:@"SCOrderTodoSubDesCell"];
        [_tableView registerClass:[SCOrderTodoListCell class] forCellReuseIdentifier:@"SCOrderTodoListCell"];
        _tableView.estimatedRowHeight = 70;
        _tableView.rowHeight = UITableViewAutomaticDimension;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor = [UIColor SCBackGroundColor];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        if (@available(iOS 11.0, *)) {
            _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        } else {
            self.automaticallyAdjustsScrollViewInsets = NO;
        }
        @Weakify(self);
        MJRefreshNormalHeader *mjHeader = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            @Strongify(self);
            [self reloadSelectData:NO];
        }];
        
        MJRefreshBackNormalFooter *mjFooter = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
            @Strongify(self);
            [self reloadSelectData:YES];
        }];
        _tableView.mj_header = mjHeader;
        _tableView.mj_footer = mjFooter;
    }
    return _tableView;
}

根据Model中返回的字段是否带有curstomerName、visitTime来加载对应的cell即可

#pragma mark table delegate
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.segmentView.selectedSegmentIndex==0?self.interactor.waitDoArrayM.count:self.interactor.completeArrayM.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSMutableArray *dataArrayM = self.segmentView.selectedSegmentIndex==0?self.interactor.waitDoArrayM:self.interactor.completeArrayM;
    SCTicketListModel *rowModel = [dataArrayM objectAtIndex:indexPath.row];;
//    rowModel.customerName = @"李杨";
//    rowModel.phone = @"13401072514";
    
    if (![rowModel.customerName isEqualToString:@""]) {
        SCOrderTodoSubDesCell *nameCell = [tableView dequeueReusableCellWithIdentifier:@"SCOrderTodoSubDesCell"];
        nameCell.selectionStyle = UITableViewCellSelectionStyleNone;
        [nameCell setCellWithModel:rowModel];
        @Weakify(self);
        nameCell.cellClickBlock = ^{
            @Strongify(self);
            [self pushTicketDetailVC:indexPath.row];
        };
        return nameCell;
        
    }else if (![rowModel.customerName isEqualToString:@""] && ![rowModel.visitTime isEqualToString:@""]){
        SCOrderTodoDesListCell *desCell = [tableView dequeueReusableCellWithIdentifier:@"SCOrderTodoDesListCell"];
        desCell.selectionStyle = UITableViewCellSelectionStyleNone;
        [desCell setCellWithModel:rowModel];
        @Weakify(self);
        desCell.cellClickBlock = ^{
            @Strongify(self);
            [self pushTicketDetailVC:indexPath.row];
        };
        return desCell;
        
    }else{
        SCOrderTodoListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SCOrderTodoListCell"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        [cell setCellWithModel:rowModel];
        @Weakify(self);
        cell.cellClickBlock = ^{
            @Strongify(self);
            [self pushTicketDetailVC:indexPath.row];
        };
        return cell;
    }
    
}

对比上一个的实现方式,这种写法确实在无形中让Controller的行数增加,不过,只要可以实现。我觉得都可以。

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 一、简介 <<UITableView(或简单地说,表视图)的一个实例是用于显示和编辑分层列出的信息的一种手段 <<...
    无邪8阅读 10,730评论 3 3
  • 1.ios高性能编程 (1).内层 最小的内层平均值和峰值(2).耗电量 高效的算法和数据结构(3).初始化时...
    欧辰_OSR阅读 29,671评论 8 265
  • 旅途总是短暂的,如一列火车汲汲而过。没有谁停留过 别说过,你看过高空,那接近太阳的地方你是否觉得火热 那俯视的目光...
    RY周周阅读 284评论 0 1
  • 起床时,窗外本是一丝亮光,拿起手机刷两下,已是带有鸟叫声的天亮。今天还是冷冻天气,本是三月好日子,无奈气温如冬天。...
    自愈乐阅读 406评论 0 0
  • 涂壁削千仞,八湾绕长林。 峰回迂水响,临顶望崖新。 取道千山秀,虽险留古今。 休言惊魄散,横卧近舒云。 ——用的新...
    溪竹青阅读 214评论 5 6