iOS 所見即所得的tableView

為啥會有LCFastBuildListKit的出現(xiàn)

我們在平時的開發(fā)中是否有遇到過這樣的界面需求,當(dāng)前界面元素非常多,種類多,常見的視圖處理方案便是使用UITableView或者UICollectionView去實(shí)現(xiàn),于是我們就在我們本身就臃腫的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;
    }
}

對于業(yè)務(wù)需求不大的場景來說無可厚非,一旦需要在頁面中新增一個Cell的時候,缺點(diǎn)一下就出來了,這個時候發(fā)現(xiàn)需要改的的地方好像略多了,需要去更改tableView:numberOfRowsInSectio:n、tableView:cellForRowAtIndexPath:等方法,而且原來對應(yīng)的indexPath全都不適用了。

然后呢我就思考了一下,有沒有比較好的方式來控制這種方式,讓我在以后的維護(hù)的時候,少做這樣的修改呢?

于是乎,我做過往tableView的數(shù)據(jù)源里裝identifier來區(qū)分cell,這樣我不再去考慮因?yàn)?code>Cell順序改變帶來的會很大程度影響我之前寫的代碼。但是后來發(fā)現(xiàn)還是不夠,我們還是需要在tableView:cellForRowAtIndexPath:或者tableView:heightForRowAtIndexPath:寫大量的判斷邏輯。

為了解決上面的尷尬,我就想封裝了一下UITableViewUICollectionView的代理方法,統(tǒng)一了對TableViewCell、CollectionViewCell、TableViewHeaderView等的描述:

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;

看到這里大家應(yīng)該都知道我在做什么了吧。。。。雖然不高級,但是用起來真的方便很多了。
最后的看一下使用之后的代碼是怎么樣的:


@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

具體使用

高度自適應(yīng)

正常情況下給ZLTableViewRowModel設(shè)置cellHeight即可,如果需要做到自適應(yīng)高度,這里需要把cellHeight設(shè)置成-1即可,(前提是cell自身已經(jīng)使用了AutoLayout撐起來了),如:

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

cell需要設(shè)置代理或者更復(fù)雜的操作

設(shè)置代理這里可以給ZLTableViewRowModel賦值delegate,cellviewController做好delegate的實(shí)現(xiàn)即可:

        [items addObject:({
            ZLTableViewRowModel *rowModel = [ZLTableViewRowModel new];
            rowModel.identifier = @"UITableViewCell4";
            rowModel.cellHeight = -1;
            rowModel.delegate = self;//這里默認(rèn)是viewController來實(shí)現(xiàn)代理方法
            rowModel;
        })];

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

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容