iOS实用功能收藏

一个controller中多个tableView的代理的封装

2022-01-21  本文已影响0人  山已几孑

闲来无事。

当一个Controller中存在多个UITableView时,我们可能会这样写:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (tableView == self.tableView1) {
        return 10;
    } else {
        return 20;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (tableView == self.tableView1) {
        return 40;
    } else {
        return 60;
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (tableView == self.tableView1) {
        UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"tableview1cell"];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:@"tableview1cell"];
        }
        cell.textLabel.text = @"I'm in tableview 1";
        return cell;
    } else {
        UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"tableview2cell"];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:@"tableview2cell"];
        }
        cell.textLabel.text = @"I'm in tableview 2";
        return cell;
    }
}

每个代理方法里面都要去进行判断,然后参照了链式编程的思路,把UITableViewDelegateUITableViewDataSource的实现提出来,放到一个公共的地方,实现的优点是:链式编程,同一个tableView的代理方法都放在了一起,无需在每个代理方法里面case;缺点是,多使用了一个tableViewDelegate的实现类。
使用代码如下:

    TableViewDelegateSource * source1 = [[TableViewDelegateSource alloc]initWithTableView:self.tableView1];
    source1.numberOfSectionsInTableView(^NSInteger(UITableView * _Nonnull tableView) {
        return 2;
    }).numberOfRowsInSection(^NSInteger(UITableView * _Nonnull tableView, NSInteger section) {
        return 20;
    }).heightForRowAtIndexPath(^CGFloat(UITableView * _Nonnull tableView, NSIndexPath * _Nonnull indexPath) {
        return 40;
    }).cellForRowAtIndexPath(^UITableViewCell * _Nonnull(UITableView * _Nonnull tableView, NSIndexPath * _Nonnull indexPath) {
        UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"tableview1cell"];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:@"tableview1cell"];
        }
        cell.backgroundColor = [[UIColor alloc]initWithRed:arc4random()%255/255.0 green:arc4random()%255/255.0 blue:arc4random()%255/255.0 alpha:1];
        cell.textLabel.text = @"tableview 1";
        return cell;
    });
    self.source1 = source1;

原理就是tableViewDelegate的实现类使用block缓存了代理的方法,运行时如果实现了block就用block,没实现就使用默认值,没啥好说的。。
有个问题cellForRowAtIndexPath的block设置完成后,后面的代码提示就没有了:(
代码在这

// 1. 声明
@interface TableViewDelegateSource : NSObject  <UITableViewDelegate,UITableViewDataSource>

@property(nonatomic, copy)TableViewDelegateSource *(^numberOfRowsInSection)(NSInteger (^)(UITableView * tableView, NSInteger section));
@property(nonatomic, copy)TableViewDelegateSource *(^cellForRowAtIndexPath)(UITableViewCell* (^)(UITableView * tableView, NSIndexPath* indexPath));
...

// 2. 声明缓存
@interface TableViewDelegateSource ()
{
    NSInteger (^kyg_numberOfRowsInSection)(UITableView * tableView, NSInteger section);
    UITableViewCell* (^kyg_cellForRowAtIndexPath)(UITableView * tableView, NSIndexPath* indexPath);
...
// 3. set 方法 
...
- (TableViewDelegateSource *(^)(NSInteger (^)(UITableView *, NSInteger)))numberOfRowsInSection {
    return ^TableViewDelegateSource *(NSInteger (^block)(UITableView *, NSInteger)) {
        self->kyg_numberOfRowsInSection = block;
        return self;
    };
}
- (TableViewDelegateSource * _Nonnull (^)(UITableViewCell * _Nonnull (^ _Nonnull)(UITableView * _Nonnull, NSIndexPath * _Nonnull)))cellForRowAtIndexPath {
    return ^TableViewDelegateSource *(UITableViewCell *(^block)(UITableView *, NSIndexPath *)) {
        self->kyg_cellForRowAtIndexPath = block;
        return self;
    };
}
...

// 4. 提供代理并设置默认值
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (kyg_numberOfRowsInSection) {
        return kyg_numberOfRowsInSection(tableView, section);
    }
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (kyg_cellForRowAtIndexPath) {
        return kyg_cellForRowAtIndexPath(tableView, indexPath);
    }
    return [UITableViewCell new];
}

挺简单的东西,就是tableView的代理怎么变的这么多了,好多不认识的。。。
demo在这里,里面就一个类

上一篇下一篇

猜你喜欢

热点阅读