ViewControllers 通常是 iOS 項目中最大的文件左敌,如何對VC進(jìn)行優(yōu)化以便于管理驶社?
一初坠、 可以把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
//調(diào)用
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;
二、將業(yè)務(wù)邏輯移到 Model 中驴剔、對model的數(shù)據(jù)處理放到model類中
比如計算tableviewCell的height省古,IM中message與user的關(guān)聯(lián)等等,對于一些復(fù)雜的model你還可以創(chuàng)建分類丧失,使得model不至于過于臃腫
三豺妓、子view的獨立
對于vc中有許多子view,比如一個直播頁面有彈幕布讹、送禮琳拭、提問、語音描验、粉絲列表白嘁、主播信息等,這一個個都可以獨立出去挠乳,在vc中完成拼裝权薯,這樣做也許會覺得代碼量變多,但是對于vc可方便管理
睡扬,對于業(yè)務(wù)邏輯的變更也可更改盟蚣,簡書代碼的重構(gòu)
四、創(chuàng)建viewModel處理數(shù)據(jù)
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;
//網(wǎng)絡(luò)請求成功回調(diào)
@property(nonatomic,copy)SZRequestCompletedBlock requestCompletedBlock;
///讀取本地數(shù)據(jù)請求回調(diào)
@property(nonatomic,copy)SZCacheCompletedBlock cacheCompletedBlock;
- (void)loadLastestPage;//刷新
- (void)loadNextPage;//加載下一頁
- (void)loadItemsWithPageNum:(NSInteger)pageNum;
//讀取本地數(shù)據(jù)
-(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 {
}
//讀取本地數(shù)據(jù)
-(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
處理網(wǎng)絡(luò)接口的數(shù)據(jù)交互卖怜、處理數(shù)據(jù)的本地緩存
五屎开、方法的重用
- 創(chuàng)建單列
- 創(chuàng)建類方法
- 繼承
- 分類