如何实现三种cell的tableview

2018-09-17  本文已影响8人  羊妞麻麻

需求如下:


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

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

项目结构如下:

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

上一篇 下一篇

猜你喜欢

热点阅读