写一个简单的表格适配器,需要持有优化。
利用指针特性,在VC、Adaptor、Cell直接进行传递model的指针,在cell中通过指针对model的具体进行修改,在VC中可以拿到更改后的值。因此具有高度耦合性。
通过block回调形式,具有高内聚特性,只需配置相应接口即可实现将消息外传功能;
使用策略模式,实现"model - cell" 一对一对应关系。使用-regsiterCell:forModelClass:
或者使用-regsiterCellNib:forModelClass:
方法即可实现model与cell的绑定,后只需在-configTableViewDataSource:
方法中对相关的model进行实例并加入分组数列中即可实现数据展示;
cell高度动态根据model中estimatedHeight
属性进行调整;
网络请求后只需根据请求结果设置model的contentUpdated
,再使用indexPathForModel:
对model所在indexPath进行查找,成功后对tableview的指定indexPath进行刷新即可更新数据的展示;
因为使用了-reloadRowsAtIndexPaths:withRowAnimation:
方法进行cell更新,可能导致cell再次创建,旧cell被移除显示池visibleCells
;
特点:动态绑定注册cell、一个model对应一个cell、方便添加表头表尾视图、动态更新model输入内容、动态计算cell高度、
pod 'JYBaseTableAdaptor'
直接安装;JYTableViewController
,可以使用内置tableview,以及其他已有功能;JYTableAdaptor
,在JYTableAdaptor
内注册cell相应的model
即可将model
与cell
进行特定的绑定;configVMAction
方法配置adaptor的行为,adaptor在cell发出相应的行为后,会将具体信息传递出来,以供操作;组-元素
的结构进行;JYTableViewController
、JYTableAdaptor
,JYBaseInputCell
、JYBaseCellModel
进行继承;viewDidLayoutSubviews
中调用[self.adaptor configSwipeButtons]
方法即可,编辑图标直接使用self.adaptor.cellEditIcon = @[[UIImage imageNamed:@"xxx"], [UIImage imageNamed:@"xxx"]];
即可;更详细的使用方式参考功能中ViewController.m部分;
添加tableview
viewDidLoad
中调用UIViewController+JYTabelAdaptor
分类方法-[ViewController configTableAdaptor]
方法,进行TableView和adaptor的懒加载;configTableAdaptor
前后对当前VC进行定制配置;@synthesize tableView = _tableView;
进行同步覆盖;指定adaptor
可以不指定其他adaptor直接使用默认的。此时需要在configTableAdaptor方法中注册cell和model,注册完成后再调用默认的configTableAdaptor方法完成配置操作;
- (void)viewDidLoad {
[super viewDidLoad];
[self configTableAdaptor];
}
- (void)configTableAdaptor {
self.adaptor.title = @"详情";
self.adaptor.key = @"TravelDetailData";
[self.adaptor regsiterCell:[xxxxCell class] forModelClass:[xxxxModel class]];
[super configTableAdaptor];
}
添加数据源
section-row
形式的数据结构进行解析;(NSArray<JYBaseGroupModel *> *)
数据结构;配置adaptor事件
增加附属动作
(NSArray<UITableViewRowAction *> *)
事件组;- (xxxAdaptor *)adaptor {
if (!_adaptor) {
_adaptor = [xxxAdaptor tableAdaptorWithTableView:self.tableView title:@"设备管理" key:@"deviceManager"];
_adaptor.dataArr = self.dataArr;
_adaptor.cellEditIcon = @[[UIImage imageNamed:@"gps_mark"], [UIImage imageNamed:@"camera"]];
_adaptor.cellEditAction = [self cellEditAction];
_adaptor.cellEditable = ^BOOL(NSindexPath *indexPath, JYTableBaseAdaptor *adaptor) {
return indexPath.section == 0;
};
}
return _adaptor;
}
- (CellEditAction)cellEditAction {
return ^NSArray<UITableViewRowAction *> * _Nullable(NSindexPath *indexPath, JYTableBaseAdaptor *adaptor, JYBaseCellModel *model) {
UITableViewRowAction *unbind = [UITableViewRowAction rowActionWithStyle:(UITableViewRowActionStyleNormal) title:@"解绑" handler:^(UITableViewRowAction * _Nonnull action, NSindexPath * _Nonnull indexPath) {
DLog(@"点击%@", action.title);
}];
UITableViewRowAction *edit = [UITableViewRowAction rowActionWithStyle:(UITableViewRowActionStyleNormal) title:@"编辑" handler:^(UITableViewRowAction * _Nonnull action, NSindexPath * _Nonnull indexPath) {
DLog(@"点击%@", action.title);
}];
return @[edit, unbind];
};
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。