擴(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)擊后做什么,都可以靈活的配置,不用再去更改代理方法了。