本篇文章是基于前两篇文章的扩充,这里没有采用链式编程,但是整体思想和第二篇类似。这里主要解决的是一个tableView包含多种类型的cell该怎么抽取的问题,本篇的代码也在github上传https://github.com/zfc769956454/ChainedDemo(下载下来运行ChainedDemo复杂tableView的抽离这个target)
1.介绍
这里主要介绍一下思想,对于复杂的tableView的抽取,采用的是cell和模型绑定的思想,有两个model类,ZFC_TableViewSectionSuperComplexModel(段模型,创建段模型的时候继承这它)和ZFC_TableViewRowSuperComplexModel(行模型,创建行模型的时候继承它)
2.核心代码
- (id)modelFromIndexPath:(NSIndexPath *)indexPath
{
return (self.sectionCount == 1)?self.dataSource[indexPath.row]:((ZFC_TableViewSectionSuperComplexModel *)self.dataSource[indexPath.section]).sectionArray[indexPath.row];
}
- (void)setTableViewDelegateAndDataSource:(UITableView *)tableView{
self.tableView = tableView;
tableView.delegate = self;
tableView.dataSource = self;
}
#pragma mark - tableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return (self.sectionCount == 1)?1:self.dataSource.count;
}
- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return (self.sectionCount == 1)?self.dataSource.count:((ZFC_TableViewSectionSuperComplexModel *)self.dataSource[section]).sectionArray.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
ZFC_TableViewRowSuperComplexModel * model = [self modelFromIndexPath:indexPath];
return model.cellHeight;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
id model = [self modelFromIndexPath:indexPath];
NSString *cellIdentifier = ((ZFC_TableViewRowSuperComplexModel *)model).cellIdentifier;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (!cell) {
[UITableViewCell registerTableView:tableView isNib:self.isCellNib cellClass:cellIdentifier];
cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
}
if(self.cellConfigBlock){
self.cellConfigBlock(indexPath,cell,model);
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
id model = [self modelFromIndexPath:indexPath];
id cell = [tableView cellForRowAtIndexPath:indexPath];
if(self.cellDidSelectedBlock){
self.cellDidSelectedBlock(indexPath, cell, model);
}
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
if(!self.headerIdetifier){
return [[UIView alloc]init];
}
UITableViewHeaderFooterView* header = [tableView dequeueReusableHeaderFooterViewWithIdentifier:self.headerIdetifier];
if (!header) {
[UITableViewHeaderFooterView registerTabelView:tableView isNib:self.isSectionHeaderNib headerFooterClass:self.headerIdetifier];
header = [tableView dequeueReusableHeaderFooterViewWithIdentifier:self.headerIdetifier];
}
if(self.headerConfigBlock){
self.headerConfigBlock(section, header,self.dataSource[section]);
}
return header;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
if(!self.headerIdetifier){
return 0.0001f;
}else{
CGFloat headerHeight = 0.0001f;
if(self.headerHeightBlock) {
headerHeight = self.headerHeightBlock(section,self.dataSource[section]);
}
return headerHeight;
}
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
if(!self.footerIdentifier){
return [[UIView alloc]init];
}
UITableViewHeaderFooterView* footer = [tableView dequeueReusableHeaderFooterViewWithIdentifier:self.footerIdentifier];
if (!footer) {
[UITableViewHeaderFooterView registerTabelView:tableView isNib:self.isSectionFooterNib headerFooterClass:self.footerIdentifier];
footer = [tableView dequeueReusableHeaderFooterViewWithIdentifier:self.footerIdentifier];
}
if(self.footerConfigBlock){
self.footerConfigBlock(section, footer,self.dataSource[section]);
}
return footer;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
if(!self.footerIdentifier){
return 0.0001f;
}else{
CGFloat footHeight = 0.0001f;
if(self.footerHeightBlock) {
footHeight = self.footerHeightBlock(section,self.dataSource[section]);
}
return footHeight;
}
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
return self.isCanDelete;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if(self.sectionCount == 1 && self.dataSource.count > indexPath.row)
{
if(self.cellDeleteBlock){
BOOL result = self.cellDeleteBlock(indexPath, self.dataSource[indexPath.row]);
if(result){
[self.dataSource removeObjectAtIndex:indexPath.row];
[self deleteCellWithIndexPath:indexPath tableView:tableView];
}else{
NSLog(@"删除失败");
[tableView reloadData];
}
}
}else
{
ZFC_TableViewSectionSuperComplexModel *secModel = self.dataSource[indexPath.section];
if(secModel.sectionArray.count > indexPath.row){
if(self.cellDeleteBlock){
BOOL result = self.cellDeleteBlock(indexPath, secModel.sectionArray[indexPath.row]);
if(result){
[secModel.sectionArray removeObjectAtIndex:indexPath.row];
[self deleteCellWithIndexPath:indexPath tableView:tableView];
}else{
NSLog(@"删除失败");
[tableView reloadData];
}
}
}
}
}
- (void)deleteCellWithIndexPath:(NSIndexPath *)indexPath tableView:(UITableView *)tableView
{
[tableView beginUpdates];
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
[tableView endUpdates];
}
这里就不再对代码做详细解释了,详细的内容在demo都有相应的解释。
3. 总结
最后,我再提一下链式创建UI和使用的小工具库ZFCChainedCreater,支持pod导入pod 'ZFCChainedCreater', '~> 1.0.2',github地址:https://github.com/zfc769956454/ZFCChainedCreater