首页投稿(暂停使用,暂停投稿)

Cell工厂设计

2017-03-09  本文已影响30人  anyurchao

Cell工厂设计

关于一个Cell工厂设计模式的 Demo

#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;
}

关键代码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

#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

另外我想说,如果学习不是为了装逼,那将毫无意义!

另外.....

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

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

上一篇 下一篇

猜你喜欢

热点阅读