美文网首页iOS开发iOS开发iOS Developer
把tableView代理从Controller中拿出来

把tableView代理从Controller中拿出来

作者: 隔壁班小明 | 来源:发表于2017-07-24 17:07 被阅读274次

controller里面代码太多,这个问题一直都有,其中最严重的估计就是tableView的代理了,每当我们在页面里面添加一个tableView,光实现代理就要添加将近50行的代码,而且这个代码在整个程序中大量的重复出现.(归功于tableView使用量太高了)。
那是不是可以将tableView的两个代理单独拿出了写,这样就可以减少controller中的代码。最好在封装一个通用的代理类,来减少代码的重复就更好了。
这当然可以,下面就说说我写的这个,写的一点也不好,就算给还没想的的同学们一个思路。

先回想一下我们平时怎么写代理的

tableView.delegate = self;
tableVIew.dataSource = self;

然后就开始再controller中写代理了,那么当然也可以把这个代理指向的self替换成一个我们自己创建的对象,这样就能让其他类实现这个tableView的代理了,(ps:这里有个坑,代理指向一个对象并不持有他,所以你实现代理的对象要做成全局属性,不然走过当前方法就被释放掉了,会导致table没代理了,后果很严重)。
我们就先创建一个实现tableView代理的类,我起名叫TableViewInterface(忽略我坑爹的名字),然后在这里面书写代理并将tableView的代理指向它这样代理就变到由它来实现了

tableView.delegate = _interface;
tableView.dataSource = _interface;

当然这个不是我们全部的目标,现在这样的话那么一个tableView就要创建一个对应的代理实现文件(突然多了好多文件,当然这样也有好处就是每个独立没有关联,错一个不会全出问题),我们现在要把TableViewInterface作为一个通用的代理类这样就一个文件,,,下面直接是代码,写的垃圾,大家看看就好。

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

typedef void(^DidSelectEvent)(NSIndexPath *indexPath);

@interface TableViewInterface : NSObject<UITableViewDelegate, UITableViewDataSource>

-(instancetype)initWithCellName:(NSString *)cellName DataSource:(NSArray *)dataSource ClickEvent:(DidSelectEvent)event;
@end
#import "TableViewInterface.h"

@interface TableViewInterface ()
@property (nonatomic, strong) NSArray * dataSource;
@property (nonatomic, copy) DidSelectEvent block;
@property (nonatomic, copy) NSString * cellName;
@end

@implementation TableViewInterface

-(instancetype)initWithCellName:(NSString *)cellName DataSource:(NSArray *)dataSource ClickEvent:(DidSelectEvent)event{
    self = [super init];
    if (self) {
        _dataSource = dataSource;
        _cellName = cellName;
        _block = event;
    }
    return self;
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
    NSString * cellIndefer = _cellName;
    Class CellClass = NSClassFromString(_cellName);
    id cell = [tableView dequeueReusableCellWithIdentifier:cellIndefer];
    if (nil == cell) {
        if ([[CellClass alloc] respondsToSelector:@selector(initWithStyle:reuseIdentifier:)]) {
            SEL selector = NSSelectorFromString(@"initWithDic:");
            cell = [[CellClass alloc] performSelector:selector withObject:cellIndefer];
        }
        SEL setModel = NSSelectorFromString(@"setModel:");
        [cell performSelector:setModel withObject:_dataSource[indexPath.row]];
    }
    return cell;
#pragma clang diagnostic pop
}

#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 45;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPat{
    if (_block) {
        _block(indexPat);
    }
    return;
}

这里面使用了performSelector:withObject:来调用方法这里有一个问题这个方法的Object只能传递一个参数,而我们创建Cell的方法有两个参数,我们只能曲线救国一下了。创建一个BaseCell。在里面写一个有一个参数的方法,然后在这个方法里调用有两个参数的方法,这样就可以解决下面这个问题了,顺便这里也要有一个设置数据的方法。以后的cell都继承这个类然后重写设置数据的方法就可以了。

#import <UIKit/UIKit.h>

@interface BaseTableViewCell : UITableViewCell
-(instancetype)initWithDic:(NSString *)identifier;
-(void)setModel:(NSDictionary *)model;
@end
#import "BaseTableViewCell.h"

@implementation BaseTableViewCell

-(instancetype)initWithDic:(NSString *)identifier{
    self = [super initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
    }
    return self;
};

-(void)setModel:(NSDictionary *)model{
    self.textLabel.text = model[@"title"];
}

@end

嗯,再看一遍使用

UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
 _interface = [[TableViewInterface alloc]initWithCellName:@"BaseTableViewCell" DataSource:@[@{@"title":@"1"},@{@"title":@"2"},@{@"title":@"3"},@{@"title":@"4"}] ClickEvent:^(NSIndexPath *indexPath){
        NSLog(@"点击%li", (long)indexPath.row);
    }];
tableView.delegate = _interface;
tableView.dataSource = _interface;
[self.view addSubview:tableView];

相关文章

网友评论

    本文标题:把tableView代理从Controller中拿出来

    本文链接:https://www.haomeiwen.com/subject/jdbbkxtx.html