zoukankan      html  css  js  c++  java
  • 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;

     

     

  • 相关阅读:
    编写你自己的单点登录(SSO)服务
    SignalR + KnockoutJS + ASP.NET MVC4 实现井字游戏
    Quartz使用总结
    Quartz.Net入门
    ASP.NET批量下载文件的方法
    Asp.Net文件的上传和下载
    AOP实践—ASP.NET MVC5 使用Filter过滤Action参数防止sql注入,让你代码安全简洁
    ASP.NET MVC4中的异步控制器
    Mybatis表关联一对多、多对一、多对多
    Xamarin android使用Sqlite做本地存储数据库
  • 原文地址:https://www.cnblogs.com/StevenHuSir/p/AppArchitecture_MVC.html
Copyright © 2011-2022 走看看