iOS-MVC架构优化

MVC 架构问题:

用户代理,业务逻辑,UI ,内部方法,代码封装导致:

VC代码过于繁重(封装)

代码耦合性过高(解耦)

1.TableView优化之封装(初始化方法和代理方法封装)

HKDataSource.h

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

typedef void(^CellConfigure)(id cell , id model, NSIndexPath *indexPath);

@interface HKDataSource : NSObject<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) NSMutableArray * dataArray ;

//自定义初始化方法
- (id)initWithIdentifier:(NSString *)identifier configureBlock:(CellConfigure)configure;

//CellIdentifier
@property (nonatomic, strong) IBInspectable NSString * cellIdentifier ;

//cellConfigure
@property (nonatomic, copy) CellConfigure cellConfigure ;

- (void)addDataArray:(NSArray*)array;

- (id)modelsAtIndexPath:(NSIndexPath*)indexPath;

@end

HKDataSource.h

#import "HKDataSource.h"

@implementation HKDataSource

- (id)initWithIdentifier:(NSString *)identifier configureBlock:(CellConfigure)configure {
    if (self = [super init]) {
        _cellIdentifier = identifier;
        _cellConfigure = [configure copy];
    }
    return self;
}

-(void)addDataArray:(NSArray *)array {
    if (!array) {
        return;
    }
    if (self.dataArray.count>0) {
        [self.dataArray removeAllObjects];
    }
    [self.dataArray addObjectsFromArray:array];
}

- (id)modelsAtIndexPath:(NSIndexPath*)indexPath {
    return self.dataArray.count > indexPath.row ? self.dataArray[indexPath.row] : nil;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return !self.dataArray ? 0 : self.dataArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:self.cellIdentifier forIndexPath:indexPath];
    id model = [self modelsAtIndexPath:indexPath];
    if (self.cellConfigure) {
        self.cellConfigure(cell, model, indexPath);
    }
    return cell;
}
@end

使用:

static NSString * reuseId = @"reuseId";
@interface ViewController ()
@property (nonatomic, strong) UITableView * tableView;
@property (nonatomic, strong) NSMutableArray * dataArray ;
@property (nonatomic, strong) HKDataSource * dataSource ;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    self.dataSource = [[HKDataSource alloc] initWithIdentifier:reuseId configureBlock:^(MVCTableViewCell *cell, Model *model, NSIndexPath *indexPath) {
        cell.model = model;
        
    }];
    [self loadData];
    [self.dataSource addDataArray:self.dataArray];
    self.view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.tableView];
    self.tableView.dataSource = self.dataSource;
    
}

- (void)loadData {
    NSArray * tempArray = @[@{@"name":@"Hank",@"imageUrl":@"http://cc",@"num":@"99"},
                            @{@"name":@"Hank",@"imageUrl":@"http://cc",@"num":@"99"},
                            @{@"name":@"Hank",@"imageUrl":@"http://cc",@"num":@"99"},
                            @{@"name":@"Hank",@"imageUrl":@"http://cc",@"num":@"99"}];
    for (int i = 0; i<tempArray.count; i++) {
        Model * model = [[Model alloc] init];
        [model setValuesForKeysWithDictionary:tempArray[i]];
        [self.dataArray addObject:model];
    }
}

-(UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
        _tableView.tableFooterView = [UIView new];
        _tableView.backgroundColor = [UIColor whiteColor];
        [_tableView registerClass:[MVCTableViewCell class] forCellReuseIdentifier:reuseId];
    }
    return _tableView;
}
@end

2.Cell耦合性强(解耦)

MVCTableViewCell * cell

cell.model = _dataArray[indexPath.row].

cell 内部(setModel:+UI赋值)[耦合性太强…]

 

 

3.cell复用 UI改变了->model不变(双向绑定)

self.model.num = self.numLabel.text;

 

 

 

转载于:https://www.cnblogs.com/StevenHuSir/p/AppArchitecture_MVC.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值