美文网首页
如何实现三种cell的tableview

如何实现三种cell的tableview

作者: 羊妞麻麻 | 来源:发表于2018-09-17 17:55 被阅读8次

需求如下:


屏幕快照 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的行数增加,不过,只要可以实现。我觉得都可以。

相关文章

网友评论

      本文标题:如何实现三种cell的tableview

      本文链接:https://www.haomeiwen.com/subject/hvmrnftx.html