创建信息TableView的优雅方式
安装
Pod
pod 'RLCellModel', '~> 1.0.0'
##为什么你需要这个
在以前,要在TableView中创建不同功能的行,你需要通过indexPath来区分行功能,就像这样
if (indexPath.row == 1) {
//do something
}else if(indexPath.row == 2){
//do something
}
}
这样是可以做到的,但当你产品经理改变一些想法,删除或删除一些行时,你将陷入修改旧逻辑的困境...
但使用这个RLCellModelManager,你只需
###好,我们来这样做
这里是有代码
首先进行一些全局设置
self.cellModelManager = [[RLCellModelManager alloc]
initWithGlobalCellIdentifier:NSStringFromClass([RLMineInfoTableViewCell class])
globalHeight:[RLMineInfoTableViewCell cellHeight]
globalReuseCellBlock:^(RLMineInfoTableViewCell *cell, RLCellModel *cellModel) {
NSDictionary *cellDict = cellModel.cellGenerateRuleDict;
NSString *title = [cellDict objectForKey:@"title"];
NSNumber *cellType = [cellDict objectForKey:@"cellType"];
cell.generalType = cellType.integerValue;
cell.titleLabel.text = title;
}
customExtractCellModelBlock:^RLCellModel *(NSArray *cellModels, NSIndexPath *indexPath) {
return [[cellModels objectAtIndex:indexPath.section]objectAtIndex:indexPath.row];
}];
self.cellModelManager.delegate = self;
self.tableView.delegate = self.cellModelManager;
self.tableView.dataSource = self.cellModelManager;
让我们看看如何实现一个行
//One Model represent a single Cell
RLCellModel *avatar = [RLCellModel new];
avatar.cellGenerateRuleDict = @{@"cellType":@(GeneralTypeAvatar),
@"title":@"头像",};
avatar.cellHeight = @([RLMineInfoTableViewCell cellHeightWithExtra]);
avatar.cellSelectedBlock = ^{
};
avatar.cellRefreshBlock = ^(id cell){
};
avatar.dynamicCellHeightBlock = ^(NSNumber *)(NSIndexPath *indexPath){
}
最后一步
NSMutableArray *datasource = [NSMutableArray arrayWithArray:@[
@[avatar, userName, realName, code,gender, qrcode],
@[phone, cardNumber, recommandPerson]]];
self.cellModelManager.cellModels = datasource;
[self.tableView reloadData];