IT博客汇
  • 首页
  • 精华
  • 技术
  • 设计
  • 资讯
  • 扯淡
  • 权利声明
  • 登录 注册

    viewcontroller瘦身之一

    键盘上的舞者发表于 2016-05-23 09:31:36
    love 0

    在一个IOS项目中,viewcontroller通常是最大的文件,并且包含了许多不必要的代码,重用率是最低的。

    我们可以尝试给viewcontroller瘦身,让他看起来不是那么的臃肿。

    今天说到的是,UITableViewDataSource。

    UITableview可能是平时写项目中用到的最多的组件了,使用它要实现它的代理协议和数据源方法,每次都是那么些东西方法controller中,看起来不舒服,我们可以给他瘦身一下。比如这样做。

    因为datasource基本上是围绕数组去做一些事,更针对的说就是围绕viewcontroller的数据数组作一些事情,我们可以把它抽出来,单独的放在一个类中,我们使用一个block来设置cell,也可以使用delegate来做这件事,这完全取决于你。

    下面上代码,举个栗子~

    //
    //  ArrayDataSource.h
    //  LrdBasicFramework
    //
    //  Created by 键盘上的舞者 on 5/21/16.
    //  Copyright © 2016 键盘上的舞者. All rights reserved.
    //  封装的一个dataSource类,可以使controller更加的轻量级
    
    #import <Foundation/Foundation.h>
    
    typedef void(^TableViewCellConfigureBlock)(id cell, id item);
    
    @interface ArrayDataSource : NSObject <UITableViewDataSource>
    
    - (id)initWithItems:(NSArray *)items
         cellIdentifier:(NSString *)identifier
         configureBlock:(TableViewCellConfigureBlock)configureBlock;
    
    - (id)itemAtIndexPath:(NSIndexPath *)indexPath;
    
    @end

    //
    //  ArrayDataSource.m
    //  LrdBasicFramework
    //
    //  Created by 键盘上的舞者 on 5/21/16.
    //  Copyright © 2016 键盘上的舞者. All rights reserved.
    //
    
    #import "ArrayDataSource.h"
    
    @interface ArrayDataSource ()
    
    @property (nonatomic, strong) NSArray *items;
    @property (nonatomic, copy) NSString *identifier;
    @property (nonatomic, copy) TableViewCellConfigureBlock configureBlock;
    
    @end
    
    @implementation ArrayDataSource
    
    - (instancetype)init {
        return nil;
    }
    
    - (id)initWithItems:(NSArray *)items cellIdentifier:(NSString *)identifier configureBlock:(TableViewCellConfigureBlock)configureBlock {
        self = [super init];
        if (self) {
            self.items = items;
            self.identifier = identifier;
            self.configureBlock = configureBlock;
        }
        return self;
    }
    
    - (id)itemAtIndexPath:(NSIndexPath *)indexPath {
        return self.items[(NSUInteger) indexPath.row];
    }
    
    #pragma mark - DataSource
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
        return self.items.count;
    }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:self.identifier forIndexPath:indexPath];
        id item = [self itemAtIndexPath:indexPath];
        self.configureBlock(cell, item);
        return cell;
    }
    @end

    然后我们可以在viewcontroller中来这样使用它

    //
    //  DemoRootController.m
    //  LrdBasicFramework
    //
    //  Created by 键盘上的舞者 on 5/21/16.
    //  Copyright © 2016 键盘上的舞者. All rights reserved.
    //
    
    #import "DemoRootController.h"
    #import "ArrayDataSource.h"
    #import "DemoFrameController.h"
    #import "DemoFouctionController.h"
    #import "DemoDIYController.h"
    #import "DemoCommUtils.h"
    
    static NSString *identifier = @"menu_cell";
    
    @interface DemoRootController ()
    
    @property (nonatomic, strong) ArrayDataSource *arrayDataSource;
    
    @end
    
    @implementation DemoRootController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        self.title = @"Demo";
        //初始化
        [self initUI];
    }
    
    - (void)initUI {
        NSArray *items = @[@"自定义控件", @"功能性拓展", @"常用工具类", @"基层框架"];
        self.arrayDataSource = [[ArrayDataSource alloc] initWithItems:items cellIdentifier:identifier configureBlock:^(UITableViewCell *cell, NSString *item) {
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            cell.textLabel.text = item;
        }];
        self.tableView.dataSource = self.arrayDataSource;
        [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:identifier];
    }
    
    #pragma mark - delegate
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
        if (indexPath.row == 0) {
            //自定义控件
            DemoDIYController *vc = [[DemoDIYController alloc] initWithStyle:UITableViewStyleGrouped];
            vc.title = [self.arrayDataSource itemAtIndexPath:indexPath];
            [self.navigationController pushViewController:vc animated:YES];
        }else if (indexPath.row == 1) {
            //功能性拓展
            DemoFouctionController *vc = [[DemoFouctionController alloc] initWithStyle:UITableViewStyleGrouped];
            vc.title = [self.arrayDataSource itemAtIndexPath:indexPath];
            [self.navigationController pushViewController:vc animated:YES];
        }else if (indexPath.row == 2) {
            //常用工具类
            DemoCommUtils *vc = [[DemoCommUtils alloc] initWithStyle:UITableViewStyleGrouped];
            vc.title = [self.arrayDataSource itemAtIndexPath:indexPath];
            [self.navigationController pushViewController:vc animated:YES];
        }else if (indexPath.row == 3) {
            //基层框架
            DemoFrameController *vc = [[DemoFrameController alloc] initWithStyle:UITableViewStyleGrouped];
            vc.title = [self.arrayDataSource itemAtIndexPath:indexPath];
            [self.navigationController pushViewController:vc animated:YES];
        }
        [tableView deselectRowAtIndexPath:indexPath animated:YES];
    }
    
    @end

    是不是看起来viewcontroller瘦了许多??



沪ICP备19023445号-2号
友情链接