ViewControllers 通常是 iOS 项目中最大的文件,如何对VC进行优化以便于管理?
一、 可以把view的datasource放到一个单独的类,比如UITableViewDataSource
@implementation ArrayDataSource
- (id)itemAtIndexPath:(NSIndexPath*)indexPath {
return items[(NSUInteger)indexPath.row];
}
- (NSInteger)tableView:(UITableView*)tableView
numberOfRowsInSection:(NSInteger)section {
return items.count;
}
- (UITableViewCell*)tableView:(UITableView*)tableView
cellForRowAtIndexPath:(NSIndexPath*)indexPath {
id cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier
forIndexPath:indexPath];
id item = [self itemAtIndexPath:indexPath];
configureCellBlock(cell,item);
return cell;
}
@end
//调用
void (^configureCell)(PhotoCell*, Photo*) = ^(PhotoCell* cell, Photo* photo) {
cell.label.text = photo.name;//给cell的subview赋值
};
photosArrayDataSource = [[ArrayDataSource alloc] initWithItems:photos
cellIdentifier:PhotoCellIdentifier
configureCellBlock:configureCell];
self.tableView.dataSource = photosArrayDataSource;
二、将业务逻辑移到 Model 中、对model的数据处理放到model类中
比如计算tableviewCell的height,IM中message与user的关联等等,对于一些复杂的model你还可以创建分类,使得model不至于过于臃肿
三、子view的独立
对于vc中有许多子view,比如一个直播页面有弹幕、送礼、提问、语音、粉丝列表、主播信息等,这一个个都可以独立出去,在vc中完成拼装,这样做也许会觉得代码量变多,但是对于vc可方便管理
,对于业务逻辑的变更也可更改,简书代码的重构
四、创建viewModel处理数据
typedef void(^SZRequestCompletedBlock)(BOOL success,NSString *error);
typedef void(^SZCacheCompletedBlock)(BOOL success,NSString *error);
@interface NewsListBaseViewModel : NSObject
@property (nonatomic, assign) BOOL refresh;
@property (nonatomic, assign) BOOL hasMoreData;
///分页
@property (nonatomic, assign) int currentPage;
@property (nonatomic, strong) NSString *firstTime;
@property (nonatomic, assign) NSInteger pageSize;
@property (nonatomic, strong) NSMutableArray *dataArray;
//网络请求成功回调
@property(nonatomic,copy)SZRequestCompletedBlock requestCompletedBlock;
///读取本地数据请求回调
@property(nonatomic,copy)SZCacheCompletedBlock cacheCompletedBlock;
- (void)loadLastestPage;//刷新
- (void)loadNextPage;//加载下一页
- (void)loadItemsWithPageNum:(NSInteger)pageNum;
//读取本地数据
-(void)getCacheDataWithPath:(NSString*)fileName;
@end
@implementation NewsListBaseViewModel
- (instancetype)init
{
if (self = [super init]) {
_currentPage = 1;
_pageSize = 10;
_firstTime = @"0";
_hasMoreData = NO;
}
return self;
}
- (void)loadLastestPage
{
self.currentPage= 1;
self.firstTime = @"0";
self.hasMoreData = NO;
[self loadItemsWithPageNum:1];
}
- (void)loadNextPage
{
self.currentPage ++;
[self loadItemsWithPageNum:self.currentPage];
}
- (void)loadItemsWithPageNum:(NSInteger)pageNum {
}
//读取本地数据
-(void)getCacheDataWithPath:(NSString*)fileName {
NSArray *array = [SZFileManager readArrayFileToLoginUserPath:fileName];
[self.dataArray addObjectsFromArray:array];
if (array.count>0) {
if (self.cacheCompletedBlock) {
self.cacheCompletedBlock(YES,nil);
}
}else {
if (self.cacheCompletedBlock) {
self.cacheCompletedBlock(NO,nil);
}
}
}
#pragma mark -
#pragma mark - getter/setter
-(NSMutableArray *)dataArray
{
if (!_dataArray)
{
_dataArray = [NSMutableArray array];
}
return _dataArray;
}
@end
处理网络接口的数据交互、处理数据的本地缓存
五、方法的重用
- 创建单列
- 创建类方法
- 继承
- 分类
网友评论