扩展这个缘由
1.uitableView是导致控制器臃肿的主要原因
2.当产品经常会对一个TableView进行cell调位置,增加cell,调整事件的时候,原来的用section来控制cell个数和展示的基本的代理方法,会显得更改代码相当的麻烦,经常是牵一发而动全身,这个时候,就需要我们对这个UItableView写的更加健壮,容扩展。
使用方面介绍截图
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 = @[@"临时有事",@"日期选错了",@"不想学了",@"其他原因"];
_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点击后做什么,都可以灵活的配置,不用再去更改代理方法了。