iOS设计模式——适配器
来源:互联网 发布:二手淘宝手机店 编辑:程序博客网 时间:2024/06/05 10:12
一 目的:
为了让客户端尽可能的通用,使用适配器模式来隔离客户端与外部参数的联系,只让客户端与适配器通信.
适配器对复杂子页面或多种cell的数据展示有很好的优势。
二 原理:
1.将网络请求下来的数据转换为模型后,放入适配器中
2.适配器对数据处理(比如高度计算)和选用对应的cell类型
3.页面数据的展示从适配器中获取模型
(这些原理在第四步的实际代码应用中会更好理解)
三 适配器类
.h代码
@interface CellDataAdapter : NSObject/** * Cell's reused identifier. */@property (nonatomic, strong) NSString *cellReuseIdentifier;/** * Data, can be nil. */@property (nonatomic, strong) id data;/** * Cell's height. */@property (nonatomic) CGFloat cellHeight;/** * Cell's type (The same cell, but maybe have different types). */@property (nonatomic) NSInteger cellType;/** * CellDataAdapter's convenient method. * * @param cellReuseIdentifiers Cell's reused identifier. * @param data Data, can be nil. * @param cellHeight Cell's height. * @param cellType Cell's type (The same cell, but maybe have different types). * * @return CellDataAdapter's object. */+ (instancetype)cellDataAdapterWithCellReuseIdentifier:(NSString *)cellReuseIdentifiers data:(id)data cellHeight:(CGFloat)cellHeight cellType:(NSInteger)cellType;#pragma mark - Optional properties./** * The tableView. */@property (nonatomic, weak) UITableView *tableView;/** * TableView's indexPath. */@property (nonatomic, weak) NSIndexPath *indexPath;
.m代码
#import "CellDataAdapter.h"@implementation CellDataAdapter+ (CellDataAdapter *)cellDataAdapterWithCellReuseIdentifier:(NSString *)cellReuseIdentifiers data:(id)data cellHeight:(CGFloat)cellHeight cellType:(NSInteger)cellType { CellDataAdapter *adapter = [[self class] new]; adapter.cellReuseIdentifier = cellReuseIdentifiers; adapter.data = data; adapter.cellHeight = cellHeight; adapter.cellType = cellType; return adapter;}@end
4.1 控制器导入 #import"CellDataAdapter.h"
4.2 将网络请求后的模型数据放到放到一个方法里处理成适配器
(dataArray放的是适配器不是模型)
4.2.1 传入模型数据
[self.tableDataArrayaddObject:[selfvtNewsModelCellAdapter:self.vtNewsModel]];
4.2.2 处理成适配器
- (CellDataAdapter *)vtNewsModelCellAdapter:(id)data {
return [CellDataAdaptercellDataAdapterWithCellReuseIdentifier:@"vtNewsModelCell"data:data cellHeight:0cellType:0];
}
5. tableview 代理方法中将适配器传给cell。
(cell包含适配器属性 :
@property (nonatomic, weak) CellDataAdapter *dataAdapter;)
customCell.dataAdapter = dataAdapter;
(其他信息比如有高度值,cell类型等,从适配器中获取)
VtNewsModel *model =self.dataAdapter.data;
- iOS设计模式——适配器
- iOS设计模式-适配器
- 设计模式—适配器模式
- 设计模式—适配器模式
- 设计模式—适配器模式
- 设计模式—适配器模式
- 设计模式—适配器模式
- 设计模式—适配器模式
- 设计模式—适配器模式
- 设计模式—适配器模式
- iOS常用设计模式——适配器Adapter
- iOS常用设计模式——适配器Adapter
- iOS设计模式-适配器模式
- iOS 设计模式 - 适配器模式
- 设计模式——适配器
- 【iOS设计模式】---- 适配器模式,观察者模式
- 设计模式——适配器设计
- 每日设计模式——适配器模式
- Aandroid 应用icon动态加载到launcher方法及framework分析
- Python-002-HelloWorld程序
- android开发小问题汇总
- sql 查询,把查出的数据作为临时表在进行查询
- PHP开发小技巧⑨—获取url中的各个参数
- iOS设计模式——适配器
- onConfigurationChanged未被调用的原因
- [学习笔记] bootstrap (三): 按钮,图片,图标
- Xshell、Xftp的使用
- android shape的使用
- Navicat快捷键选择当前行
- Spring事务管理(详解+实例)
- stress —linux系统下工作量和压力测试工具
- JNI开发----复习曾经学习过的C语言