tableView 通過(guò)代理瘦身

tableView 通過(guò)代理瘦身

iOS開發(fā)中,用的最多的也就tableView,collectionView了,開發(fā)中我們一般將創(chuàng)建view,獲取數(shù)據(jù)通過(guò)VC來(lái)完成的,一旦業(yè)務(wù)邏輯增加,這樣操作會(huì)讓VC顯得特別臃腫,曾今我就見過(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還是僅僅處理邏輯。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容