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
處理網絡接口的數據交互、處理數據的本地緩存
五、方法的重用
- 創建單列
- 創建類方法
- 繼承
- 分類