# TableViewModelModule (opens new window)

# 该组件是基于IOS代理原理开发的一套列表基础控件,只要编写少许胶水代码即可完成一个列表配置,并能跟后端网络数据进行无缝衔接。做到快速开发业务的能力,极大地提高了开发者效率,减少因列表配置产生的错误。

# 该组件集成至所有素+系列IOS应用内,其组件内部抽象程度和调用程度较高。

# 说明

  • 通过使用Block机制编写胶水代码,简化步骤即可完成列表数据配置
  • 集成上下刷新功能,跟网络组件无缝集成,实现业务快速开发

# 安装

在项目 Podfile 文件内添加

pod 'TableViewModelModule'

在项目目录下执行脚本

pod install

插件即可集成至项目内

# 使用

生成 UITableView 对象

UITableView *t = [[UITableView alloc]init];
[SJUIKit sj_tableView:t withDelegate:nil];

创建TableDelegate (opens new window) 对象,此时tableView对象中已经含有Array数据集合,只需要配置CellModel关系集合,该类还提供了很多API,方便使用者调用。

 TableDelegate *tableDelegate = 、
     [[TableDelegate alloc] initWithNumberOfRowsInSection:^NSInteger(UITableView *tableView, TableDelegate *delegate, NSInteger section) {

        return tableView.refreshDataArray.count;
                
      } cellForRowAtIndexPath:^UITableViewCell *(UITableView *tableView, TableDelegate *delegate, NSIndexPath *indexPath) {
                
         Cell *cell =  (Cell *)[delegate.listMapping cellAtIndexPath:indexPath fromDataSource:tableView.refreshDataArray];
         Model *model = [tableView.refreshDataArray objectOrNilAtIndex:indexPath.row];
         cell.model = model;
         return cell;
      }];

随后需要对CellModel进行映射关联,下面的代码保证一种Cell对应的是一个Model对象。

//modelMapping
TableListMapping *listMapping = [[TableListMapping alloc] initScrollView:t];
tableDelegate.listMapping = listMapping;
[t buildWithTableDelegate:tableDelegate];
listMapping[@"Model"] = Cell.class;

此时TableView对象已经完成了配置即可加入到图层上,如果需要对每个cell进行高度配置则可以编写以下胶水代码。

tableDelegate.heightForRowAtIndexPath = ^CGFloat(UITableView *tableView, TableDelegate *delegate, NSIndexPath *indexPath) {
   NSObject *model =  [tableView.refreshDataArray objectAtIndex:indexPath.row];
   @weakify(model);
   return   [tableView qmui_heightForCellWithIdentifier:NSStringFromClass(model.class) cacheByIndexPath:indexPath configuration:^(__kindof OnewCell *cell) {
      @strongify(model);
      cell.model = (Model *)model;
   }];
  };
};
最后更新时间: 11/25/2021, 9:51:20 AM