iOS UIviewContoller 瘦身大运动

989 阅读6分钟
原文链接: www.jianshu.com
  • 前言

    通常一个项目中ViewController的代码最多,复用度最低,随着项目功能完善,一个臃肿的ViewContoller便出现了,如果这个时候来了新人接手项目!!!而你就是这个新人的话。Oh my god!看着可能多达上千行的代码,相信我,你会有一种蛋蛋的忧伤。感觉无从下手...一种无力感油然而生,感觉身体被掏空=。=

被掏空

来来来小伙子,喝一瓶


好的你明天可以不用来上班了

  • 本例实现一个从网络下载数据,显示到tableView中的demo最终只需要在viewController中写下代码便可实现(可能不是最优的,如果您有更好的建议,欢迎讨论,纠正我。):

    self.dataSouce = [PQTBDataSource dataSourceWith:_dataArray identifier:CELLIDENTIFIER cellConfigBlock:^(TableViewCell * _Nullable cell, id  _Nullable item) {
          [cell configCellWithIem:item];
      }];
      self.myTableView.dataSource = self.dataSouce;
      [self.myTableView registerNib:[UINib nibWithNibName:@"TableViewCell" bundle:nil] forCellReuseIdentifier:CELLIDENTIFIER];
    
      typeof(self) weakSelf = self;
      [NetWorkManager dataTaskWith:URL completionHandler:^(NSArray *itemsArray) {
          [weakSelf.dataSouce pq_updateWithArray:itemsArray];
          [weakSelf.myTableView reloadData];
      }];
  • 1 优化TableView的Datasource

    . 先看优化前和优化之后的对比:
    优化前

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _array.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    cell.textLabel.text = _array[indexPath.row];
    return cell;
}

优化后,且可复用

self.dataSouce = [PQTBDataSource dataSourceWith:_dataArray identifier:CELLIDENTIFIER cellConfigBlock:^(TableViewCell * _Nullable cell, id  _Nullable item) {
        //这里更新设置你的cell
    }];
    self.myTableView.dataSource = self.dataSouce;
  • 开始之前还扯一扯:在项目中如果你用到了两个以上的tableView的时候你会发现你一定会存在重复的代码,而且还是没啥意义的。比如
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _array.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    cell.textLabel.text = _array[indexPath.row];

    return cell;
}

先不用看里面写了啥!!!,基本雷同是不是?
你的tableview还会有

这里仅仅是开个玩笑哈 会报错的。
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return [@"fuck" integerValue];
}

对于上面的代码,完全可以提取出来,让你的ViewController看起来舒服点。
让他减减肥...

开始干活了

  • 1 第一步,建一个类,并且实现datasource的方法。

    建一个类,名字你随意,我干了

  • 2 第二步,继承
    既然把刚才上面列举的方法提取出来,这里肯定是我们自己去实现了。
    先实现方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 在某组里面有多少个cell
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
   每个cell的样式 还要从复用池取出cell需要Identifier
}

然后你就发现这里我们需要一个数组,所以这里需要

  • 一个数组

  • 一个identifier

    在考虑一下有必要公开么?笔者认为是没有必要公开的,所以就写在.m文件中
    //传入之后不允许用户在外面随意更改数据
    @property (nonatomic,strong,readwrite) NSMutableArray * _Nullable valuesArray;
    @property (nonatomic,copy) NSString * identifier;
    上面的代码中你会发现有一个readwrite,这个是干啥的呢?主要是配合.h文件中的
    @interface PQTBDataSource : NSObject 
    //传入之后不允许用户在外面随意更改数据
    @property (nonatomic,strong,readonly) NSMutableArray * _Nullable valuesArray;

    如果你还是不知道啥意思?看图


    现在你只能读,不能写。但是又想在.m中使用self.valuesArray的话可以这样写


    到目前为止,我们已经确定了两个东西,一个是数组,一个是Identifier。
    还有一个问题:

    这个时候每一个的cell要怎么设置,内部不知道,cell可以是很多种,自定义的xib画的,类名都不一样,里面完成不了对cell的设置。所以我们需要我们得交给外面去处理,这里你想使用delegate或者block随便你,不过笔者比较喜欢block,代码内聚。

到目前为止,我们需要的东西就知道了:

  • 数组
  • identifier
  • 一个block
    于是就可以写一个类似于:
+ (nonnull instancetype)dataSourceWith:(nullable NSArray *)values identifier:(nullable NSString *)identifier cellConfigBlock:(nullable void(^)( id _Nullable cell,id _Nullable item))block;
- (nonnull instancetype)initWithDataSource:(nullable NSArray *)values identifier:(nullable NSString *)identifier  cellConfigBlock:(nullable void(^)(id _Nullable cell,id _Nullable item))block;

的方法。
到这里datasource基本上就设置好了,把方法都实现一下

+ (nonnull instancetype)dataSourceWith:(nullable NSArray *)values identifier:(nullable NSString *)identifier  cellConfigBlock:(nullable void(^)( id _Nullable cell,id _Nullable item))block{
    return [[self alloc]initWithDataSource:values identifier:identifier cellConfigBlock:block];
}
- (nonnull instancetype)initWithDataSource:(nullable NSArray *)values identifier:(nullable NSString *)identifier  cellConfigBlock:(nullable void(^)(id _Nullable cell,id _Nullable item))block
{
    self = [super init];
    if (self) {
        self.identifier = identifier ;
        self.valuesArray = [NSMutableArray arrayWithArray:values];
        self.cellConfigBlock = [block copy];
    }
    return self;
}
- (id _Nullable )itemWithIndexPath:(NSIndexPath *)indexPath{
    return self.valuesArray[indexPath.row];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.valuesArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    id cell = [tableView dequeueReusableCellWithIdentifier:self.identifier forIndexPath:indexPath];
    id item = [self itemWithIndexPath:indexPath];
    self.cellConfigBlock(cell,item);
    return cell;
}

于是你ViewController中的tableView就可以这样子设置:

self.dataSouce = [PQTBDataSource dataSourceWith:_dataArray identifier:CELLIDENTIFIER cellConfigBlock:^(TableViewCell * _Nullable cell, id  _Nullable item) {
        //这里更新设置你的cell
    }];
    self.myTableView.dataSource = self.dataSouce;

这样子你的ViewController就几行代码就实现了之前数十行要实现且没有啥子意义的代码,并且你还可以在其他的tableView中去使用。

  • 2 封装一下数据请求,这部分的代码没有viewController没有必要去管理,他只需要一个结果就好了,

既然只需要一个结果,那么我们最终就放回一个结果(不管请求失败还是成功都只是一个结果)。但是我们并不知道什么时候会请求完成或者失败,不能一直等待,所以一定是异步进行请求,请求完成就通过block回调。

  • 2.1 分析一下需要什么才可以请求一个数据
    -URL
    这里我们不考虑太复杂的情况了,随便请求一点数据好了。
    所以可以封装一个方法大致如下:
+ (void)dataTaskWith:(NSString *)url completionHandler:(void(^)(NSArray * itemsArray))block;

这里可能你会注意到,为什么我返回的是一个数组:是这样子的,viewController同样不需要知道我们数据转化为模型的过程,一样的理念,他只需要结果,一个有用的结果就行了。
实现代码

+ (void)dataTaskWith:(NSString *)url completionHandler:(void(^)(NSArray * itemsArray))block{
    NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
    AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];

    NSURL *URL = [NSURL URLWithString:url];
    NSURLRequest *request = [NSURLRequest requestWithURL:URL];

    NSURLSessionDataTask *dataTask = [manager dataTaskWithRequest:request completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
        NSDictionary * dict = (NSDictionary *)responseObject;
        NSMutableArray * array = [NSMutableArray array];
        NSArray * objects = dict[@"subjects"];
        for (NSDictionary * dict in objects) {
            [array addObject:[TableViewModel tableViewModelWith:dict]];
        }
        if (block) {
            block(array);
        }
    }];
    [dataTask resume];
}

在这里我们同时还生成了一个Model,最后我们只需要把模型返回就好。
模型

+ (instancetype)tableViewModelWith:(NSDictionary *)dict;
+ (instancetype)tableViewModelWith:(NSDictionary *)dict{
    TableViewModel * model = [[self alloc]init];
    [model setValuesForKeysWithDictionary:dict];
    return model;
}

- (void)setValue:(id)value forUndefinedKey:(NSString *)key{

    if ([key isEqualToString:@"id"] == YES) {
        _ID = value;
    }
}

- (void)setImages:(NSDictionary *)images{
    _images = images[@"small"];
}

到这里,我们基本上就已经实现了代码啦!!!!
于是乎,我们在viewController中还要写上:

typeof(self) weakSelf = self;
    [NetWorkManager dataTaskWith:URL completionHandler:^(NSArray *itemsArray) {
        [weakSelf.dataSouce pq_updateWithArray:itemsArray];
        [weakSelf.myTableView reloadData];
    }];

当我们的数据请求回来刷新tableView一次。

  • 3.最后一点,很多时候我们需要实现右滑功能,这里实现了一下,右滑删除功能,同样不用再viewController中去写代码,代码量没有增加,功能却实现了,而且这样子做的好处就是除了了问题就可以知道是datasource不用再viewController中苦苦寻找。

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
    return YES;
}



- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    [self.valuesArray removeObjectAtIndex:indexPath.row];
    [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}

The end
demo github.com/codepgq/PQS…
码字不易,看完如果对你有帮助,赞一个就是对笔者莫大的鼓励.