可配置的UITableVIew,適用于數(shù)據(jù)源不多的Tableview

擴(kuò)展這個緣由

1.uitableView是導(dǎo)致控制器臃腫的主要原因
2.當(dāng)產(chǎn)品經(jīng)常會對一個TableView進(jìn)行cell調(diào)位置,增加cell,調(diào)整事件的時候,原來的用section來控制cell個數(shù)和展示的基本的代理方法,會顯得更改代碼相當(dāng)?shù)穆闊?jīng)常是牽一發(fā)而動全身,這個時候,就需要我們對這個UItableView寫的更加健壯,容擴(kuò)展。

使用方面介紹截圖

1.tableView代理賦值給一個專門處理TableView的DataSource的類

    self.viewModel = [[DTSetTableViewModel alloc]init];
    self.viewModel.dtDelegate = self;
    self.tableView.delegate = self.viewModel;
    self.tableView.dataSource = self.viewModel;
    [self initViewModel];
- (void)initViewModel
{
    [self.viewModel.sectionModelArray removeAllObjects];
    [self.viewModel.sectionModelArray addObject:[self sectionOne]];
}

2.添加組,一個組里面可以再添加具體的cell

- (DTTableViewSectionModel *)sectionOne
{
    DTTableViewSectionModel *sectionModel = [[DTTableViewSectionModel alloc]init];
    sectionModel.headerHeight = 40;
    sectionModel.footerHeight = 0.1;
    sectionModel.defaultHeaderTitle = @"請選擇取消原因";
    sectionModel.headerBackGroudColor = [UIColor colorWithString:@"F5F6F7"];
    sectionModel.defaultHeaderTextFont = [UIFont boldSystemFontOfSize:14];
    NSMutableArray *arrm =[NSMutableArray array];
    for (DTSelectCancelCellItem *item in [self cellDataModelArray]) {
        DTTableViewCellModel *cellMode = [self cellModewithItem:item];
        [arrm addObject:cellMode];
    }
    [sectionModel.cellModelArray addObjectsFromArray:arrm];
    return sectionModel;
}

- (NSMutableArray *)cellDataModelArray
{
    if (!_cellDataModelArray) {
        NSArray *arr = @[@"臨時有事",@"日期選錯了",@"不想學(xué)了",@"其他原因"];
        _cellDataModelArray = [NSMutableArray array];
        for (NSString *str  in arr) {
            DTSelectCancelCellItem *item = [[DTSelectCancelCellItem alloc]init];
            item.title = str;
            item.reasonString = str;
            [_cellDataModelArray addObject:item];
        }
    }
    return _cellDataModelArray;
}

- (DTTableViewCellModel *)cellModewithItem:(DTSelectCancelCellItem *)item
{
    DTWeakify(self);
    DTTableViewCellModel *cellModel = [[DTTableViewCellModel alloc]init];
    cellModel.height = item.isShowTextView== YES ? 164:44;
    cellModel.renderBlock = ^UITableViewCell *(NSIndexPath *indexPath, UITableView *tableView) {
        DTSelectCancelCell *cell = [tableView dequeueReusableCellWithIdentifier:DTSelectCancelCellID  forIndexPath:indexPath];
        cell.item= item;
        return cell;
    };
    cellModel.selectionBlock = ^(NSIndexPath *indexPath, UITableView *tableView) {
        DTStrongify(self);
        [self changeHeightWithIndexPath:indexPath  withTableView:tableView];
    };
    return cellModel;
}

這樣哪個組做什么事情,哪個cell點(diǎn)擊后做什么,都可以靈活的配置,不用再去更改代理方法了。

這個是一個簡單的tableView的數(shù)據(jù)源的遷移封裝,使用簡單,沒有提供Demon ,可以直接下載我的這個類,拖進(jìn)去就可以直接使用了

文件地址:https://github.com/jsonmall/GPSettingTableView.git

?著作權(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)容