Cell工廠設計
關于一個Cell工廠設計模式的 Demo
- Model層
- .首先建立 BaseModel并繼承他建立OneModel,TwoModel,ThreeModel
- 在BaseModel中聲明實現方法,根據傳過來的數據源(字典)判斷,并且進行對應Model映射
關鍵代碼BaseModel
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
@interface BaseModel : NSObject
//根據傳過來的數據源(字典)判斷,并且進行對應Model映射
+(instancetype)modelWithDictionary:(NSDictionary *)dic;
@end
#import "BaseModel.h"
#import "OneModel.h"
#import "TwoModel.h"
#import "ThreeModel.h"
@implementation BaseModel
//根據傳過來的數據源(字典)判斷,并且進行對應Model映射
+(instancetype)modelWithDictionary:(NSDictionary *)dic {
BaseModel *model = nil;
if ([dic[@"tag"] isEqualToString:@"Top News"]) {
model = [OneModel new];
}else if ([dic[@"tag"] isEqualToString:@"imgextra"]){
model = [TwoModel new];
}else if ([dic[@"tag"] isEqualToString:@"music"]){
model = [ThreeModel new];
}
//字典對Model賦值
[model setValuesForKeysWithDictionary:dic];
return model;
}
- View 層
- 首先建立 BaseCell并繼承他建立OneModelCell,TwoModelCell,ThreeModelCell
- 在 BaseCell 中聲明實現方法
cellWithModel:
根據Model返回對應的Cell,cellHeightWithModel:
跟根據模型返回cell的高度 - 在OneModelCell,TwoModelCell,ThreeModelCell中重寫他的方法
cellHeightWithModel:
并通過 setModel: 方法對其進行賦值
關鍵代碼BaseCell
#import <UIKit/UIKit.h>
@class BaseModel;
@interface BaseCell : UITableViewCell
@property (nonatomic, strong)BaseModel * model;
//簡單工廠
//根據Model返回對應的Cell
+(instancetype)cellWithModel:(BaseModel *)model;
//返回cell的高度
+(CGFloat)cellHeightWithModel:(BaseModel *)baseModel;
@end
#import "BaseCell.h"
#import "BaseModel.h"
#import <objc/runtime.h>
@implementation BaseCell
//簡單工廠
//根據Model返回對應的Cell
+(instancetype)cellWithModel:(BaseModel *)model {
NSString *modelName = [NSString stringWithUTF8String:object_getClassName(model)];
NSString *cellName = [NSString stringWithFormat:@"%@Cell",modelName];
BaseCell *cell = [[objc_getClass(cellName.UTF8String) alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellName];
return cell;
}
//返回cell的高度
+(CGFloat)cellHeightWithModel:(BaseModel *)baseModel{
NSString *modelName = [NSString stringWithUTF8String:object_getClassName(baseModel)];
NSString *cellName = [NSString stringWithFormat:@"%@Cell",modelName];
return [objc_getClass(cellName.UTF8String) cellHeightWithModel:baseModel];
}
@end
- Controller層
- 數據請求并傳給
BaseModel
,baseMode 通過modelWithDictionary:
對Model映射
*通過當前模型類名獲取沖泳池中的 cell,若為空則根據Model返回對應的Cell - 根據 model 返回cell的高度
關鍵代碼RootTableViewController
#import "RootTableViewController.h"
#import "BaseModel.h"
#import "BaseCell.h"
@interface RootTableViewController ()
@property (nonatomic,strong)NSMutableArray *dataArray;
@end
@implementation RootTableViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
NSArray *array = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"data" ofType:@"plist"]];
for (NSDictionary *dic in array) {
BaseModel *model = [BaseModel modelWithDictionary:dic];
[self.dataArray addObject:model];
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
BaseModel *model = self.dataArray[indexPath.row];
BaseCell *cell = [tableView dequeueReusableCellWithIdentifier:[NSString stringWithUTF8String:object_getClassName(model)]];
if (nil == cell){
cell = [BaseCell cellWithModel:model];
}
cell.model = model;
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
BaseModel *model = self.dataArray[indexPath.row];
return [BaseCell cellHeightWithModel:model];
}
#pragma mark- =====================getter method=====================
- (NSMutableArray *)dataArray {
if (!_dataArray){
_dataArray = [[NSMutableArray alloc] initWithCapacity:10];
}
return _dataArray;
}
@end
另外我想說,如果學習不是為了裝逼,那將毫無意義!
另外.....
我的愿望是.......
世界和平.........