viewModel优化tableView思路整理

2019-08-28  本文已影响0人  学习之路

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

1、创建ViewModel 类:
2、创建sectionModel 类:
3、设置cellModel类:
4、在Controller中使用:
- (YZSTableViewModel*)viewModel {
   if(!_viewModel) {
       _viewModel = [[YZSTableViewModel alloc] init];
       self.tableView.dataSource = _viewModel;
       self.tableView.delegate = _viewModel;
       _viewModel.delegate = self;
   }
   return _viewModel;
}
- (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;
}
上一篇 下一篇

猜你喜欢

热点阅读