Cell工廠設(shè)計(jì)
關(guān)于一個(gè)Cell工廠設(shè)計(jì)模式的 Demo
- Model層
- .首先建立 BaseModel并繼承他建立OneModel,TwoModel,ThreeModel
- 在BaseModel中聲明實(shí)現(xiàn)方法,根據(jù)傳過來的數(shù)據(jù)源(字典)判斷,并且進(jìn)行對應(yīng)Model映射
關(guān)鍵代碼BaseModel
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
@interface BaseModel : NSObject
//根據(jù)傳過來的數(shù)據(jù)源(字典)判斷,并且進(jìn)行對應(yīng)Model映射
+(instancetype)modelWithDictionary:(NSDictionary *)dic;
@end
#import "BaseModel.h"
#import "OneModel.h"
#import "TwoModel.h"
#import "ThreeModel.h"
@implementation BaseModel
//根據(jù)傳過來的數(shù)據(jù)源(字典)判斷,并且進(jìn)行對應(yīng)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 中聲明實(shí)現(xiàn)方法
cellWithModel:
根據(jù)Model返回對應(yīng)的Cell,cellHeightWithModel:
跟根據(jù)模型返回cell的高度 - 在OneModelCell,TwoModelCell,ThreeModelCell中重寫他的方法
cellHeightWithModel:
并通過 setModel: 方法對其進(jìn)行賦值
關(guān)鍵代碼BaseCell
#import <UIKit/UIKit.h>
@class BaseModel;
@interface BaseCell : UITableViewCell
@property (nonatomic, strong)BaseModel * model;
//簡單工廠
//根據(jù)Model返回對應(yīng)的Cell
+(instancetype)cellWithModel:(BaseModel *)model;
//返回cell的高度
+(CGFloat)cellHeightWithModel:(BaseModel *)baseModel;
@end
#import "BaseCell.h"
#import "BaseModel.h"
#import <objc/runtime.h>
@implementation BaseCell
//簡單工廠
//根據(jù)Model返回對應(yīng)的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層
- 數(shù)據(jù)請求并傳給
BaseModel
,baseMode 通過modelWithDictionary:
對Model映射
*通過當(dāng)前模型類名獲取沖泳池中的 cell,若為空則根據(jù)Model返回對應(yīng)的Cell - 根據(jù) model 返回cell的高度
關(guān)鍵代碼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
另外我想說,如果學(xué)習(xí)不是為了裝逼虐译,那將毫無意義!
另外.....
我的愿望是.......
世界和平.........