一个基于MVVM的TableView组件化实现方案

2022-12-04,,,

AITableView

https://github.com/chentoo/AITableView

cocoapods:

pod ‘AITableView’

做什么用?

这是一个简化UITableView使用方式的一个尝试,不需要再实现UI TableView繁多的delegate和datasource方法,不需要重复实现繁多的cell的if else / switch 逻辑,只需要简单的配置过程,你就可以轻松的驾驭和布局TableView。

这是一个基于MVVM思想的尝试,并且最终拼装成为TableView的元素不再是一个个的cell,而是一个个轻量的ViewModel(我们暂且叫他CellModel)。每个CellModel对应值唯一的一种cell,CellModel只拥有属性, 且每一个属性都将直接决定Cell的展示效果。

长远来看,你可以轻松的组建一个你自己的CellModel库。在同一个TableView的不同的逻辑情况下,或者不同的TableView中,你可以根据自己的需求选出合适的CellModel,配置属性,拼装组建即可。

怎么使用?

首先使用AITabelView,你不需要更改你原本的Cell的基类和model的基类,只需根据需要实现特定的Protocal即可。

1、 你的每个Cell需要实现AITableViewCellProtocal,并实现对应方法。你应该把你所有的Cell UI元素的配置放在

1

- (void)AIConfigureWithModel:(DemoNameCellModel *)model1

并在类方法

1

+ (CGFloat)AIHeightWithModel:(id)model1

返回你的cell的高度。

比如:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

#import "DemoTableViewNameCell.h"
#import "AITableViewProtocal.h"
#import "DemoNameCellModel.h"
 
@interface DemoTableViewNameCell ()  @property (nonatomic, strong) UILabel *nameLabel;
@end
 
@implementation DemoTableViewNameCell
 
#pragma mark - AITableViewCellProtocal
 
- (void)AIConfigureWithModel:(DemoNameCellModel *)model
{    self.nameLabel.text = model.name;
}
 
+ (CGFloat)AIHeightWithModel:(id)model
{    return 30.0f;
}
@end

2、接下来去写一个能够唯一对应这个cell的cellModel,它可以继承自任何基类,也不需实现任何Protocal。但是需要保证的是,cell可以根据它所唯一对应的cellModel完成所有配置。

需要特别注意的是,每个CellModel类,对应且唯一对应一个Cell类,也即不同的Cell Class 不能对应同一个CellModel类。

完成cell对应的cellModel后,使用下面的方法,去绑定他们。

1
2
3
4

//AITableView.h
 
- (void)bindModelClass:(Class)modelClass withCellClass:(Class)cellClass;
- (void)bindModelClass:(Class)modelClass withCellNibClass:(Class)cellNibClass;1234

特别的,如果一个cell的逻辑不依赖外部的变化而变化,那么他可能不需要一个cellModel去控制。那么有下面的特别方法去绑定它们。

当然其实我并不建议这么做,因为从长远组建CellModel库的角度来讲,拥有一个唯一的cellModel可能更容易管理和使用。

1
2
3
4

//AITableView.h
 
- (void)bindStaticCellWithCellClass:(Class)cellClass;
- (void)bindStaticCellWithCellNibClass:(Class)cellNibClass;1234

3、最后使用下面的方法就可以更新你的TableView了。

1

[self.tableview updateTabelViewWithModels:@[model, sModel, sModel, model]];1

4、如果你需要包含不同的section,或者需要headview footerview,那么你可以使用: 
AITableViewSection 去构建。

1
2
3
4
5
6
7
8
9

@interface AITableViewSection : NSObject
 
@property (strong, nonatomic) id headerModel;
@property (strong, nonatomic) id footerModel;
@property (strong, nonatomic) NSArray *cellModels;
 
+ (instancetype)sectionWithHeaderModel:(id)sectionHeaderModel
                           footerModel:(id)sectionFooterModel
                            cellModels:(NSArray *)cellModels;@end1234567891011

并且使用下面的方法去更新TableView

[self.tableview updateTableViewWithSections:@[aiSection, aiSection, aiSection]];1

5、更多的请参看完整示例代码。

如何接收cell 点击事件?

提供了三种方式:

    AITableView的 delegate

    AITableView的 Block

    model 的 protocal block(可以方便的配置在model身上)

具体请看示例代码。

一个基于MVVM的TableView组件化实现方案的相关教程结束。

《一个基于MVVM的TableView组件化实现方案.doc》

下载本文的Word格式文档,以方便收藏与打印。