iOS知识IOS三人行程序

iOS UIviewContoller瘦身大运动

2016-08-06  本文已影响775人  Codepgq

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

被掏空

来来来小伙子,喝一瓶


好的你明天可以不用来上班了
 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];
    }];

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

- (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;
- (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看起来舒服点。
让他减减肥...

开始干活了

.

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 在某组里面有多少个cell
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
   每个cell的样式 还要从复用池取出cell需要Identifier
}

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

在考虑一下有必要公开么?笔者认为是没有必要公开的,所以就写在.m文件中

//传入之后不允许用户在外面随意更改数据
@property (nonatomic,strong,readwrite) NSMutableArray * _Nullable valuesArray;
@property (nonatomic,copy) NSString * identifier;

上面的代码中你会发现有一个readwrite,这个是干啥的呢?主要是配合.h文件中的

@interface PQTBDataSource : NSObject <UITableViewDataSource>
//传入之后不允许用户在外面随意更改数据
@property (nonatomic,strong,readonly) NSMutableArray * _Nullable valuesArray;

如果你还是不知道啥意思?看图
![现在你只能读,不能写。但是又想在.m中使用self.valuesArray的话可以这样写](http:https://img.haomeiwen.com/i1940927/db83b6518a22f1a6.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
到目前为止,我们已经确定了两个东西,一个是数组,一个是Identifier。
还有一个问题:
##### 这个时候每一个的cell要怎么设置,内部不知道,cell可以是很多种,自定义的xib画的,类名都不一样,里面完成不了对cell的设置。所以我们需要我们得交给外面去处理,这里你想使用delegate或者block随便你,不过笔者比较喜欢block,代码内聚。

到目前为止,我们需要的东西就知道了:
- 数组
- identifier
- 一个block
于是就可以写一个类似于:

的方法。
到这里datasource基本上就设置好了,把方法都实现一下
于是你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
这里我们不考虑太复杂的情况了,随便请求一点数据好了。
所以可以封装一个方法大致如下:

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

在这里我们同时还生成了一个Model,最后我们只需要把模型返回就好。
模型
实现
到这里,我们基本上就已经实现了代码啦!!!!
于是乎,我们在viewController中还要写上:

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

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

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

The end
demo <https://github.com/codepgq/PQSeparationCode>
码字不易,看完如果对你有帮助,赞一个就是对笔者莫大的鼓励.
上一篇 下一篇

猜你喜欢

热点阅读