把tableView代理从Controller中拿出来

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];
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 136,188评论 19 139
  • iOS网络架构讨论梳理整理中。。。 其实如果没有APIManager这一层是没法使用delegate的,毕竟多个单...
    yhtang阅读 10,706评论 1 23
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 14,746评论 4 61
  • 我情愿, 独自承受所有的痛苦; 把好运留到和你在一起的时候。 -------题记 得闲逛一逛展销会,竟然与诺诺不期...
    兰兮阅读 2,571评论 13 14
  • 1.第一卷内容中,你有哪些问题?是否有不同的看法? 关于毒物兴奋效应能增加身体的反脆弱性,我认为是植物性的毒性才会...
    A美丽人生阅读 2,507评论 6 1

友情链接更多精彩内容