tableView 通過(guò)代理瘦身
iOS開(kāi)發(fā)中剧董,用的最多的也就tableView,collectionView了,開(kāi)發(fā)中我們一般將創(chuàng)建view跳座,獲取數(shù)據(jù)通過(guò)VC來(lái)完成的,一旦業(yè)務(wù)邏輯增加泣矛,這樣操作會(huì)讓VC顯得特別臃腫疲眷,曾今我就見(jiàn)過(guò)一個(gè)VC成千上萬(wàn)行代碼。那么我們能不能減輕一下VC的負(fù)擔(dān)呢您朽?答案是肯定的狂丝,今天我們通過(guò)代理,讓VC瘦身。
通過(guò)代理給控制器瘦身
給控制器瘦身錢几颜,這里我們要?jiǎng)?chuàng)建一個(gè)工具類倍试,用于處理tableview代理方法,其實(shí)就是講tableview delegate方法交給其他類去處理蛋哭,VC僅僅處理一點(diǎn)邏輯就好县习。
//定義一個(gè)全局block
typedef void (^selectIndexPathBlock)(NSIndexPath *indexPath);
@interface testProtocol : NSObject<UITableViewDelegate,UITableViewDataSource>
+(instancetype)crateUsuallyTableObjWith:(NSArray *)tableListDatas selectIndexPathBlock:(selectIndexPathBlock)selectIndexPath;
@end
#import "testProtocol.h"
@interface testProtocol ()
@property(nonatomic,strong)NSArray *datas;
@property(nonatomic,copy)selectIndexPathBlock selectIndexPathB;
@end
@implementation testProtocol
//類型方法,使用起來(lái)更加方便
+(instancetype)crateUsuallyTableObjWith:(NSArray *)tableListDatas selectIndexPathBlock:(selectIndexPathBlock)selectIndexPath
{
return [[[self class]alloc]initWithData:tableListDatas selectIndexPathBlock:selectIndexPath];
}
-(instancetype)initWithData:(NSArray *)datas selectIndexPathBlock:(selectIndexPathBlock)selectIndexPath{
if (self = [super init]) {
self.datas = datas;
self.selectIndexPathB = selectIndexPath;
}
return self;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.datas.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifier = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
}
cell.textLabel.text = self.datas[indexPath.row];
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
//通過(guò)block回調(diào)谆趾,傳遞列表點(diǎn)擊的位置躁愿,用于跳轉(zhuǎn)等操作
self.selectIndexPathB(indexPath);
}
@end
代理頁(yè)面實(shí)現(xiàn)部分
#import "secViewController.h"
#import "testProtocol.h"
@interface secViewController ()
@property(nonatomic,strong)UITableView *tableView;
@property(nonatomic,strong)testProtocol *protoTEst;
@end
@implementation secViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"secView";
self.view.backgroundColor = [UIColor whiteColor];
self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 375, 568)];
[self.view addSubview:self.tableView];
self.protoTEst = [testProtocol crateUsuallyTableObjWith:@[@"ggg",@"zzz",@"hhh"] selectIndexPathBlock:^(NSIndexPath * _Nonnull indexPath) {
//回調(diào)用于i跳轉(zhuǎn)
NSLog(@"%ld",indexPath.row);
}];
self.tableView.delegate = self.protoTEst;
self.tableView.dataSource = self.protoTEst;
}
-(void)dealloc{
NSLog(@"sec dealloc");
}
@end
最后這樣處理,vc僅僅幾行代碼就解決了棺妓。對(duì)于網(wǎng)絡(luò)數(shù)據(jù)這里沒(méi)處理攘已,下篇文章講解對(duì)于網(wǎng)絡(luò)數(shù)據(jù)處理封裝,封裝一個(gè)工具類處理怜跑,vc還是僅僅處理邏輯样勃。