iOS大咖说

tableView 通过代理瘦身

2020-09-08  本文已影响0人  bugLife丶

tableView 通过代理瘦身

iOS开发中,用的最多的也就tableView,collectionView了,开发中我们一般将创建view,获取数据通过VC来完成的,一旦业务逻辑增加,这样操作会让VC显得特别臃肿,曾今我就见过一个VC成千上万行代码。那么我们能不能减轻一下VC的负担呢?答案是肯定的,今天我们通过代理,让VC瘦身。

通过代理给控制器瘦身

给控制器瘦身钱,这里我们要创建一个工具类,用于处理tableview代理方法,其实就是讲tableview delegate方法交给其他类去处理,VC仅仅处理一点逻辑就好。

//定义一个全局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
//类型方法,使用起来更加方便
+(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];
    //通过block回调,传递列表点击的位置,用于跳转等操作
    self.selectIndexPathB(indexPath);
}
@end

代理页面实现部分

#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) {
        //回调用于i跳转
        NSLog(@"%ld",indexPath.row);
    }];
    self.tableView.delegate =  self.protoTEst;
    self.tableView.dataSource =  self.protoTEst;
}

-(void)dealloc{
    NSLog(@"sec dealloc");
}
@end

最后这样处理,vc仅仅几行代码就解决了。对于网络数据这里没处理,下篇文章讲解对于网络数据处理封装,封装一个工具类处理,vc还是仅仅处理逻辑。

上一篇 下一篇

猜你喜欢

热点阅读