美文网首页
iOS 所见即所得的tableView

iOS 所见即所得的tableView

作者: jgyhc | 来源:发表于2019-06-28 11:26 被阅读0次

为啥会有LCFastBuildListKit的出现

我们在平时的开发中是否有遇到过这样的界面需求,当前界面元素非常多,种类多,常见的视图处理方案便是使用UITableView或者UICollectionView去实现,于是我们就在我们本身就臃肿的ViewController里开始写UITableViewdelegatedataSource方法了,然后我们就可能会看到tableView:cellForRowAtIndexPath:方法里的代码如下:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSInteger section = indexPath.section;
    if (section == 0) {
        if (indexPath.row == 0) {
            OrderHeaderCell *cell = [tableView dequeueReusableCellWithIdentifier:@"OrderHeaderCell"];
            cell.status = self.data.State;
            return cell;
        } else {
            FBGroupPurchaseCell *cell = [tableView dequeueReusableCellWithIdentifier:@"FBGroupPurchaseCell"];
            cell.list = self.productList;
            return cell;
        }
    } else if (section == 2) {
        
        NSArray *arr = self.resultArr[0];
        if (arr.count == 0) {
            VouchersWaitPayCell *cell = [tableView dequeueReusableCellWithIdentifier:@"VouchersWaitPayCell"];
            return cell;
        } else {
            OrderInfoCell *cell = [tableView dequeueReusableCellWithIdentifier:@"OrderInfoCell"];
            cell.model = arr[indexPath.row];
            return cell;
        }
    } else if (section == 1) {
        OrderInfoCell *cell = [tableView dequeueReusableCellWithIdentifier:@"OrderInfoCell"];
        return cell;
    } else {
        OrderInfoCell *cell = [tableView dequeueReusableCellWithIdentifier:@"OrderInfoCell"];
        NSArray *arr = self.resultArr[section - 2];
        cell.model = arr[indexPath.row];
        if (section == 3 && indexPath.row == arr.count - 1) {
            [cell addline];
        }
        return cell;
    }
    
}

然后tableView:heightForRowAtIndexPath:方法下可能就是


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSInteger section = indexPath.section;
    if (section == 0) {
        if (indexPath.row == 0) {
            return 60;
        } else {
            return 105;
        }
    } else if (section == 1) {
        return 0;
    } else if (section == 2){
        NSArray *arr = self.resultArr[0];
        if (arr.count > 0) {
            return 30;
        } else {
            return 110;
        }
    } else {
        return 30;
    }
}

对于业务需求不大的场景来说无可厚非,一旦需要在页面中新增一个Cell的时候,缺点一下就出来了,这个时候发现需要改的的地方好像略多了,需要去更改tableView:numberOfRowsInSectio:ntableView:cellForRowAtIndexPath:等方法,而且原来对应的indexPath全都不适用了。

然后呢我就思考了一下,有没有比较好的方式来控制这种方式,让我在以后的维护的时候,少做这样的修改呢?

于是乎,我做过往tableView的数据源里装identifier来区分cell,这样我不再去考虑因为Cell顺序改变带来的会很大程度影响我之前写的代码。但是后来发现还是不够,我们还是需要在tableView:cellForRowAtIndexPath:或者tableView:heightForRowAtIndexPath:写大量的判断逻辑。

为了解决上面的尴尬,我就想封装了一下UITableViewUICollectionView的代理方法,统一了对TableViewCellCollectionViewCellTableViewHeaderView等的描述:

TableViewCell

@property (nonatomic, copy) NSString *identifier;

@property (nonatomic, assign) CGFloat cellHeight;

@property (nonatomic, strong) id data;

CollectionViewCell:

@property (nonatomic, copy) NSString *identifier;

@property (nonatomic, assign) CGSize cellSize;

@property (nonatomic, strong) id data;

TableView的组:

@property (nonatomic, copy) NSString *headerIdentifier;

@property (nonatomic, copy) NSString *footerIdentifier;

@property (nonatomic, strong) NSArray<ZLTableViewRowModel *> *items;

@property (nonatomic, strong) id headerData;

@property (nonatomic, strong) id footerData;

@property (nonatomic, assign) CGFloat headerHeight;

@property (nonatomic, assign) CGFloat footerHeight;

CollectionView的组:


@property (nonatomic, copy) NSString *headerIdentifier;

@property (nonatomic, copy) NSString *footerIdentifier;

@property (nonatomic, strong) NSArray<ZLCollectionViewRowModel *> *items;

@property (nonatomic, strong) id headerData;

@property (nonatomic, strong) id footerData;

@property (nonatomic, assign) CGSize headerSize;

@property (nonatomic, assign) CGSize footerSize;

@property (nonatomic, assign) UIEdgeInsets insets;

@property (nonatomic, assign) CGFloat minimumLineSpacing;

@property (nonatomic, assign) CGFloat minimumInteritemSpacing;

看到这里大家应该都知道我在做什么了吧。。。。虽然不高级,但是用起来真的方便很多了。
最后的看一下使用之后的代码是怎么样的:


@implementation LCTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.manager reloadData];
}

- (NSArray<ZLTableViewSectionModel *> *)dataSource:(ZLTableViewDelegateManager *)manager {
    NSMutableArray *sectionModels = [NSMutableArray array];
    
    //组1
    [sectionModels addObject:({
        ZLTableViewSectionModel *sectionModel = [ZLTableViewSectionModel new];
        NSMutableArray *items = [NSMutableArray array];
        sectionModel.items = items;
        [items addObject:({
            ZLTableViewRowModel *rowModel = [ZLTableViewRowModel new];
            rowModel.identifier = @"UITableViewCell1";
            rowModel.cellHeight = 40;
            rowModel;
        })];
        
        [items addObject:({
            ZLTableViewRowModel *rowModel = [ZLTableViewRowModel new];
            rowModel.identifier = @"UITableViewCell3";
            rowModel.cellHeight = 100;
            rowModel;
        })];
        
        [items addObject:({
            ZLTableViewRowModel *rowModel = [ZLTableViewRowModel new];
            rowModel.identifier = @"UITableViewCell4";
            rowModel.cellHeight = 90;
            rowModel;
        })];
        
        [items addObject:({
            ZLTableViewRowModel *rowModel = [ZLTableViewRowModel new];
            rowModel.identifier = @"UITableViewCell5";
            rowModel.cellHeight = 50;
            rowModel;
        })];
        sectionModel;
    })];
    
    //组2
    [sectionModels addObject:({
        ZLTableViewSectionModel *sectionModel = [ZLTableViewSectionModel new];
        NSMutableArray *items = [NSMutableArray array];
        sectionModel.items = items;
        [items addObject:({
            ZLTableViewRowModel *rowModel = [ZLTableViewRowModel new];
            rowModel.identifier = @"UITableViewCell6";
            rowModel.cellHeight = 60;
            rowModel;
        })];
        
        [items addObject:({
            ZLTableViewRowModel *rowModel = [ZLTableViewRowModel new];
            rowModel.identifier = @"UITableViewCell2";
            rowModel.cellHeight = 100;
            rowModel;
        })];
        
        [items addObject:({
            ZLTableViewRowModel *rowModel = [ZLTableViewRowModel new];
            rowModel.identifier = @"UITableViewCell7";
            rowModel.cellHeight = 30;
            rowModel;
        })];
        
        [items addObject:({
            ZLTableViewRowModel *rowModel = [ZLTableViewRowModel new];
            rowModel.identifier = @"UITableViewCell4";
            rowModel.cellHeight = 70;
            rowModel;
        })];
        sectionModel;
    })];
    
    
    return sectionModels;
}

- (ZLTableViewDelegateManager *)manager {
    if (!_manager) {
        _manager = [[ZLTableViewDelegateManager alloc] init];
        _manager.delegate = self;
        _manager.tableView = self.tableView;
    }
    return _manager;
}

@end

具体使用

高度自适应

正常情况下给ZLTableViewRowModel设置cellHeight即可,如果需要做到自适应高度,这里需要把cellHeight设置成-1即可,(前提是cell自身已经使用了AutoLayout撑起来了),如:

        [items addObject:({
            ZLTableViewRowModel *rowModel = [ZLTableViewRowModel new];
            rowModel.identifier = @"UITableViewCell4";
            rowModel.cellHeight = -1;
            rowModel;
        })];

cell需要设置代理或者更复杂的操作

设置代理这里可以给ZLTableViewRowModel赋值delegatecellviewController做好delegate的实现即可:

        [items addObject:({
            ZLTableViewRowModel *rowModel = [ZLTableViewRowModel new];
            rowModel.identifier = @"UITableViewCell4";
            rowModel.cellHeight = -1;
            rowModel.delegate = self;//这里默认是viewController来实现代理方法
            rowModel;
        })];

demo:https://github.com/jgyhc/LCFastBuildListKit

相关文章

网友评论

      本文标题:iOS 所见即所得的tableView

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