有時候我們的控制器會非常龐大再来,一部分原因就是控制器要獲取數(shù)據(jù),可能包含從網(wǎng)絡中加載數(shù)據(jù)磷瘤,這時候我們需要把獲取dataSource這部分代碼分離出來芒篷,創(chuàng)建一個新的類。
1. tableView分離dataSource
分離UITableViewController中的dataSource到一個單獨的類采缚,并實現(xiàn)UITableViewDataSource協(xié)議及其方法针炉。
ArrayDataSource.h
typedef void (^TableViewCellConfigureBlock)(id cell, id item);
@interface ArrayDataSource : NSObject <UITableViewDataSource>
- (id)initWithItems:(NSArray *)anItems
cellIdentifier:(NSString *)aCellIdentifier
configureCellBlock:(TableViewCellConfigureBlock)aConfigureCellBlock;
- (id)itemAtIndexPath:(NSIndexPath *)indexPath;
@end
ArrayDataSource.m
@interface ArrayDataSource ()
@property (nonatomic, strong) NSArray *items;
@property (nonatomic, copy) NSString *cellIdentifier;
@property (nonatomic, copy) TableViewCellConfigureBlock configureCellBlock;
@end
@implementation ArrayDataSource
- (id)init
{
return nil;
}
- (id)initWithItems:(NSArray *)anItems
cellIdentifier:(NSString *)aCellIdentifier
configureCellBlock:(TableViewCellConfigureBlock)aConfigureCellBlock
{
self = [super init];
if (self) {
self.items = anItems;
self.cellIdentifier = aCellIdentifier;
self.configureCellBlock = [aConfigureCellBlock copy];
}
return self;
}
- (id)itemAtIndexPath:(NSIndexPath *)indexPath
{
return self.items[(NSUInteger) indexPath.row];
}
#pragma mark UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.items.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:self.cellIdentifier
forIndexPath:indexPath];
id item = [self itemAtIndexPath:indexPath];
self.configureCellBlock(cell, item);
return cell;
}
@end
使用該dataSource,只需要initWithItems即可扳抽,并把該dataSource賦值給tableView.dataSource
- (void)setupTableView
{
TableViewCellConfigureBlock configureCell = ^(PhotoCell *cell, Photo *photo) {
[cell configureForPhoto:photo];
};
NSArray *photos = [AppDelegate sharedDelegate].store.sortedPhotos;
self.photosArrayDataSource = [[ArrayDataSource alloc] initWithItems:photos
cellIdentifier:PhotoCellIdentifier
configureCellBlock:configureCell];
self.tableView.dataSource = self.photosArrayDataSource;
[self.tableView registerNib:[PhotoCell nib] forCellReuseIdentifier:PhotoCellIdentifier];
}
2. 從UIViewController中分離網(wǎng)絡請求
單獨定義一個UIViewControllerData類篡帕,實現(xiàn)網(wǎng)絡請求殖侵。該類實現(xiàn)UIViewController中定義的協(xié)議,在指定時刻去加載數(shù)據(jù)镰烧,進行網(wǎng)絡請求拢军,并把獲取的數(shù)據(jù)通過調(diào)離的block返回給控制器,控制器在進行其他操作(如tableView reload)
.h
@interface UIViewControllerData : NSObject <RefreshableListViewControllerDataSource>
+ (instancetype)sharedInstance;
@end
.m
@implementation UIViewControllerData
+ (instancetype)sharedInstance {
static UIViewControllerData *instance;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
instance = [[UIViewControllerData alloc] init];
});
return instance;
}
- (void)refreshableListViewController:(RefreshableListViewController *)controller loadDataFrom:(NSUInteger)offset limit:(NSUInteger)limit withUserToken:(NSString *)token completion:(void (^)(NSArray *, BOOL))completionBlock fail:(void (^)(NSError *))failBlock {
//網(wǎng)絡請求數(shù)據(jù)
[UserAPI query:param userToken:token completion:^(NSArray<UserProfileSimplify> *userProfiles, BOOL hasMore) {
if (completionBlock) {
completionBlock(userProfiles,hasMore);
}
} error:^(ServerAPIError *error) {
DDLogDebug(@"---MYLOG:出錯啦怔鳖!---");
}];
} error:failBlock];
}
@end