閱讀代碼SigmaTableViewModel嫡秕,寫(xiě)下看到的思路丸凭。如有侵權(quán)钙蒙,請(qǐng)聯(lián)系我刪除茵瀑,謝謝
1、創(chuàng)建ViewModel 類:
- 設(shè)置YZSTableViewModelDelegate 協(xié)議躬厌,協(xié)議方法為空
- 設(shè)置sectionModel數(shù)組马昨,sectionModel中有個(gè)屬性數(shù)組存放單獨(dú)的cellModel竞帽;
- 設(shè)置delegate:YZSTableViewModelDelegate,用于避免tableView的代理方法未實(shí)現(xiàn)的警告
- 獲取sectionModel的key方法
- 獲取cellModel的key方法
- 對(duì)sectionModel進(jìn)行增鸿捧、刪屹篓、刷新方法(非線程安全方法)
- 對(duì)cellModel進(jìn)行增、刪匙奴、刷新方法(非線程安全方法)
2堆巧、創(chuàng)建sectionModel 類:
- 設(shè)置cellModel數(shù)組,數(shù)組存放單獨(dú)的cellModel;
- 設(shè)置sectionModel的屬性
- 設(shè)置sectionModel的key泼菌,用于識(shí)別不同的sectionModel
- 聲明header谍肤、footer的回調(diào);用于設(shè)置headerView哗伯、footerView.
3荒揣、設(shè)置cellModel類:
- 設(shè)置cellModel屬性
- 設(shè)置cellModel的key,用于識(shí)別不同的cellModel
- 聲明返回cell的回調(diào):在回調(diào)中設(shè)置cell焊刹,必須設(shè)置方法
- 聲明返回cell高度的回調(diào):在回調(diào)中設(shè)置cell高度
- 聲明即將顯示的cell的回調(diào):
- 聲明即將顯示的cell的回調(diào):
- 聲明將要選擇的cell的回調(diào):
- 聲明將要取消選擇的cell的回調(diào):
- 聲明選擇的cell的回調(diào):
- 聲明取消選擇的cell的回調(diào):
- 聲明提交編輯的cell的回調(diào):
- 聲明應(yīng)該突出顯示的高度回調(diào)
- 系任。。虐块。赋除。 等等 tableView代理方法的回調(diào)
4、在Controller中使用:
- 遵循代理<YZSTableViewModelDelegate>
- 設(shè)置屬性viewModel非凌;
- (YZSTableViewModel*)viewModel {
if(!_viewModel) {
_viewModel = [[YZSTableViewModel alloc] init];
self.tableView.dataSource = _viewModel;
self.tableView.delegate = _viewModel;
_viewModel.delegate = self;
}
return _viewModel;
}
- 在viewDidLoad方法中實(shí)現(xiàn)模型數(shù)據(jù)加載:
- (void)reloadViewModel {
[self.viewModel.sectionModelArray removeAllObjects];
//shop info section
{
YZSTableViewSectionModel *sectionModel = [[YZSTableViewSectionModel alloc] init];
[self.viewModel.sectionModelArray addObject:sectionModel];
sectionModel.headerTitle = @"Shop Info";
static NSString *CellIdentifier = @"ShopCell";
YZWeak(self);
//shop details entrance
YZSTableViewCellModel *cellModel = [[YZSTableViewCellModel alloc] init];
[sectionModel.cellModelArray addObject:cellModel];
cellModel.height = 44;
cellModel.renderBlock = ^UITableViewCell * _Nonnull(NSIndexPath * _Nonnull indexPath, UITableView * _Nonnull tableView) {
YZStrong(self)
UITableViewCell *cell = [self reusableCellWithId:CellIdentifier inView:tableView];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.selectionStyle = UITableViewCellSelectionStyleDefault;
id<ShopModuleService> shopModule = BFModule(ShopModuleService);
cell.imageView.image = shopModule.shopLogo;
NSString *text = BFStr(@"Shop Name: %@", shopModule.shopName);
cell.textLabel.text = text;
return cell;
};
cellModel.selectionBlock = ^(NSIndexPath * _Nonnull indexPath, UITableView * _Nonnull tableView) {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
UIViewController *vc = [Bifrost handleURL:kRouteShopDetail];
if (vc) {
[self.navigationController pushViewController:vc animated:YES];
}
};
//revenue info
cellModel = [[YZSTableViewCellModel alloc] init];
[sectionModel.cellModelArray addObject:cellModel];
cellModel.height = 44;
cellModel.renderBlock = ^UITableViewCell * _Nonnull(NSIndexPath * _Nonnull indexPath, UITableView * _Nonnull tableView) {
YZStrong(self)
UITableViewCell *cell = [self reusableCellWithId:CellIdentifier inView:tableView];
NSString *text = BFStr(@"Revenue: ¥%.2f", [BFModule(ShopModuleService) shopRevenue]);
cell.textLabel.text = text;
cell.accessoryType = UITableViewCellAccessoryNone;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
};
}
//goods info section
{
YZSTableViewSectionModel *sectionModel = [[YZSTableViewSectionModel alloc] init];
[self.viewModel.sectionModelArray addObject:sectionModel];
sectionModel.headerTitle = @"Popular Goods";
static NSString *CellIdentifier = @"GoodsCell";
YZWeak(self);
//popular goods list
NSArray *list = [BFModule(GoodsModuleService) popularGoodsList];
for (id<GoodsProtocol> goods in list) {
YZSTableViewCellModel *cellModel = [[YZSTableViewCellModel alloc] init];
[sectionModel.cellModelArray addObject:cellModel];
cellModel.height = 44;
cellModel.renderBlock = ^UITableViewCell * _Nonnull(NSIndexPath * _Nonnull indexPath, UITableView * _Nonnull tableView) {
YZStrong(self)
UITableViewCell *cell = [self reusableCellWithId:CellIdentifier inView:tableView];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
NSString *text = BFStr(@"%@ : ¥%.2f", goods.name, goods.price);
cell.textLabel.text = text;
return cell;
};
cellModel.selectionBlock = ^(NSIndexPath * _Nonnull indexPath, UITableView * _Nonnull tableView) {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
NSString *routeURL = BFStr(@"%@?%@=%@", kRouteGoodsDetail, kRouteGoodsDetailParamId, goods.goodsId);
UIViewController *vc = [Bifrost handleURL:routeURL];
if (vc) {
[self.navigationController pushViewController:vc animated:YES];
}
};
}
//all goods entry
YZSTableViewCellModel *cellModel = [[YZSTableViewCellModel alloc] init];
[sectionModel.cellModelArray addObject:cellModel];
cellModel.height = 44;
cellModel.renderBlock = ^UITableViewCell * _Nonnull(NSIndexPath * _Nonnull indexPath, UITableView * _Nonnull tableView) {
YZStrong(self)
UITableViewCell *cell = [self reusableCellWithId:CellIdentifier inView:tableView];
cell.textLabel.text = @"More Goods...";
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
};
cellModel.selectionBlock = ^(NSIndexPath * _Nonnull indexPath, UITableView * _Nonnull tableView) {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
UIViewController *vc = [Bifrost handleURL:kRouteAllGoodsList];
if (vc) {
[self.navigationController pushViewController:vc animated:YES];
}
};
}
[self.tableView reloadData];
}
- (UITableViewCell*)reusableCellWithId:(NSString*)identifier
inView:(UITableView*)tableView {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:identifier];
}
return cell;
}