ZLCellDataSource
mvvm,mvp 的应用。为 viewcontroller 节省瘦身,把 tableview 和 collectionview 的 datasource 提取出来,可以节省 1/3 的代码量。
版本
0.4.1
- 适配 UITableViewDataSource 的 cell
0.4.0
- 增加多 section 的情况下,数组可变的场景
- 增加多 section 的情况下,配置 collectionView 的头部和尾部视图
0.3.0
- 增加多个 cell 的操作,需要实现代理方法
0.2.3
- 多 section 的操作加入了复杂类型,具体需要指定 ZLSectionModel
firstClassName : 为第一层数组的数据 class 类名 secondVariate : 为第二层数组对应的元素变量名
0.2.2
- 增加了多 section 的 class 数组未传全,自动以最后一个 class 来填充全
0.2.1
- 修正了多 section 的 bug
0.2.0
- 增加多 section 的 datasource 提取
支持 CocoaPod 导入
pod 'ZLCellDataSource'
ZLCellDataSource
把 UITableViewDataSource 和 UICollectionView 的代理提取出来,使用者只需要在 UIViewController 类中定义好 ZLCellDataSource* dataSource 的对象,并实现它的 block 即可,并且主要要将 dataSource 赋值给 UITableViewDataSource 的 dataSource。同理 UICollectionView 也是如此。
@interface ViewController ()
@property(nonatomic,strong)NSMutableArray* array;
//注意dataSource一定要声明为类的成员变量,才能生效。
@property(nonatomic,strong)ZLCellDataSource* dataSource;
@property(nonatomic,strong)UITableView* tableView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.array = [NSMutableArray new];
for (int i=0; i<50; i++) {
[self.array addObject:[NSString stringWithFormat:@"行数-----%d",i]];
}
/*
block原来是(id cell, id item, NSIndexPath *indexPath)
需要将cell和item的id类型替换成自己实际使用的cell类和item类
如以下我是UITableViewCell*类和NSString*类
*/
self.dataSource = [[ZLCellDataSource alloc]initWithItems:self.array cellIdentifier:@"ZLCell" configureCellBlock:^(UITableViewCell* cell, NSString* item, NSIndexPath *indexPath) {
cell.textLabel.text = item;
cell.textLabel.textColor = [UIColor redColor];
}];
self.tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
/*
以下两步一定要
将self.dataSource赋值过来,才能生效
必须注册cell的类
*/
self.tableView.dataSource = self.dataSource;
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"ZLCell"];
[self.view addSubview:self.tableView];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
如需使用,请点击以下链接
ZLSectionDataSource同样,实现函数过程也是类似的。
- (instancetype)initWithItems:(NSArray *)items
cellIdentifier:(NSArray<NSString*> *)cellIdentifiers
configureCellBlock:(CellConfigureBlock)configureCellBlock;
与单个section不同,cellIdentifiers需要传入数组,也可以只传递一个元素。ZLSectionDataSource会自动帮您将所有setion匹配为同一个cellIdentifiers。
举例:
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.users = [NSMutableArray new];
for (int i=0; i<5; i++) {
User* user = [User new];
user.name = @"czl";
user.phone = @"1111";
user.age = 20;
NSMutableArray* arr = [NSMutableArray new];
for (int j=0; j<3; j++) {
Address* addr = [Address new];
addr.province = @"fujian";
addr.city = @"xiamen";
addr.address = @"123123123123";
[arr addObject:addr];
}
user.addresses = arr;
[self.users addObject:user];
}
ZLSectionModel* model = [ZLSectionModel new];
model.firstClassName = @"User";
model.secondVariate = @"addresses";
self.dataSource = [[ZLSectionDataSource alloc]initWithItems:self.users cellIdentifier:@[@"UserCell"] cellClasses:@[UserCell.class] modelDic:model configureCellBlock:^(UserCell* cell, Address* item, NSIndexPath *indexPath) {
cell.address = item;
}];
self.tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
self.tableView.dataSource = self.dataSource;
self.tableView.delegate = self;
[self.tableView registerClass:UserCell.class forCellReuseIdentifier:@"UserCell"];
[self.tableView registerClass:UserHeader.class forHeaderFooterViewReuseIdentifier:@"UserHeader"];
[self.view addSubview:self.tableView];
}
请点击以下链接以了解详细信息
ZLBaseViewModel实现viewModel时,可以将网络请求等数据处理分离出来
使用时请继承ZLBaseViewModel。将网络请求函数编写在您定义的viewModel类中,由viewController调用。