viewModel优化tableView思路整理

阅读代码SigmaTableViewModel,写下看到的思路。如有侵权,请联系我删除,谢谢

1、创建ViewModel 类:
  • 设置YZSTableViewModelDelegate 协议,协议方法为空
  • 设置sectionModel数组,sectionModel中有个属性数组存放单独的cellModel;
  • 设置delegate:YZSTableViewModelDelegate,用于避免tableView的代理方法未实现的警告
  • 获取sectionModel的key方法
  • 获取cellModel的key方法
  • 对sectionModel进行增、删、刷新方法(非线程安全方法)
  • 对cellModel进行增、删、刷新方法(非线程安全方法)
2、创建sectionModel 类:
  • 设置cellModel数组,数组存放单独的cellModel;
  • 设置sectionModel的属性
  • 设置sectionModel的key,用于识别不同的sectionModel
  • 声明header、footer的回调;用于设置headerView、footerView.
3、设置cellModel类:
  • 设置cellModel属性
  • 设置cellModel的key,用于识别不同的cellModel
  • 声明返回cell的回调:在回调中设置cell,必须设置方法
  • 声明返回cell高度的回调:在回调中设置cell高度
  • 声明即将显示的cell的回调:
  • 声明即将显示的cell的回调:
  • 声明将要选择的cell的回调:
  • 声明将要取消选择的cell的回调:
  • 声明选择的cell的回调:
  • 声明取消选择的cell的回调:
  • 声明提交编辑的cell的回调:
  • 声明应该突出显示的高度回调
  • 。。。。 等等 tableView代理方法的回调
4、在Controller中使用:
  • 遵循代理
  • 设置属性viewModel;
- (YZSTableViewModel*)viewModel {
   if(!_viewModel) {
       _viewModel = [[YZSTableViewModel alloc] init];
       self.tableView.dataSource = _viewModel;
       self.tableView.delegate = _viewModel;
       _viewModel.delegate = self;
   }
   return _viewModel;
}
  • 在viewDidLoad方法中实现模型数据加载:
- (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 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 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;
}

你可能感兴趣的:(viewModel优化tableView思路整理)