Cell工廠設(shè)計(jì)

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

祭出 Demo

另外我想說,如果學(xué)習(xí)不是為了裝逼虐译,那將毫無意義!

另外.....

我的愿望是.......

世界和平.........

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末驴娃,一起剝皮案震驚了整個(gè)濱河市削饵,隨后出現(xiàn)的幾起案子岩瘦,更是在濱河造成了極大的恐慌,老刑警劉巖葵孤,帶你破解...
    沈念sama閱讀 222,627評論 6 517
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件担钮,死亡現(xiàn)場離奇詭異,居然都是意外死亡尤仍,警方通過查閱死者的電腦和手機(jī)箫津,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,180評論 3 399
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來宰啦,“玉大人苏遥,你說我怎么就攤上這事∩哪#” “怎么了田炭?”我有些...
    開封第一講書人閱讀 169,346評論 0 362
  • 文/不壞的土叔 我叫張陵,是天一觀的道長漓柑。 經(jīng)常有香客問我教硫,道長叨吮,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 60,097評論 1 300
  • 正文 為了忘掉前任瞬矩,我火速辦了婚禮茶鉴,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘景用。我一直安慰自己涵叮,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 69,100評論 6 398
  • 文/花漫 我一把揭開白布伞插。 她就那樣靜靜地躺著割粮,像睡著了一般。 火紅的嫁衣襯著肌膚如雪媚污。 梳的紋絲不亂的頭發(fā)上舀瓢,一...
    開封第一講書人閱讀 52,696評論 1 312
  • 那天,我揣著相機(jī)與錄音杠步,去河邊找鬼氢伟。 笑死,一個(gè)胖子當(dāng)著我的面吹牛幽歼,可吹牛的內(nèi)容都是我干的朵锣。 我是一名探鬼主播,決...
    沈念sama閱讀 41,165評論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼甸私,長吁一口氣:“原來是場噩夢啊……” “哼诚些!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起皇型,我...
    開封第一講書人閱讀 40,108評論 0 277
  • 序言:老撾萬榮一對情侶失蹤诬烹,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后弃鸦,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體绞吁,經(jīng)...
    沈念sama閱讀 46,646評論 1 319
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,709評論 3 342
  • 正文 我和宋清朗相戀三年唬格,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了家破。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,861評論 1 353
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡购岗,死狀恐怖汰聋,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情喊积,我是刑警寧澤烹困,帶...
    沈念sama閱讀 36,527評論 5 351
  • 正文 年R本政府宣布,位于F島的核電站乾吻,受9級特大地震影響髓梅,放射性物質(zhì)發(fā)生泄漏拟蜻。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 42,196評論 3 336
  • 文/蒙蒙 一女淑、第九天 我趴在偏房一處隱蔽的房頂上張望瞭郑。 院中可真熱鬧辜御,春花似錦鸭你、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,698評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至碳抄,卻和暖如春愉老,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背剖效。 一陣腳步聲響...
    開封第一講書人閱讀 33,804評論 1 274
  • 我被黑心中介騙來泰國打工嫉入, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人璧尸。 一個(gè)月前我還...
    沈念sama閱讀 49,287評論 3 379
  • 正文 我出身青樓咒林,卻偏偏與公主長得像,于是被迫代替她去往敵國和親爷光。 傳聞我的和親對象是個(gè)殘疾皇子垫竞,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,860評論 2 361

推薦閱讀更多精彩內(nèi)容