iOS设置代理,不同的代理方法是可以分别由父类或者子类实现的, 同一个代理协议的不同方法也是可以分别在子类或者父类中实现的

TPAskanswerSinglePageView 是LBVerticalPageViewCell
的子类, 父类和子类实现了不同代理的代理方法
父类实现了tableView的 UIScrollViewdelegate, 子类实现了tableViewDelegate,都可以正常执行


@interface TPAskanswerSinglePageView : LBVerticalPageViewCell

@end

父类中创建了tableView 并设置了代理

- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.bounds), CGRectGetHeight(self.bounds)) style:UITableViewStylePlain];
        if (@available(iOS 11.0, *)) {
            _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        } else {
            // Fallback on earlier versions
        }
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:NSStringFromClass([UITableViewCell class])];
        _tableView.tableFooterView = self.loadMoreIndicatorView;
    }
    return _tableView;
}

然而父类并没有实现tableView的代理方法
只是
实现了scrolllView 代理方法


@interface LBVerticalPageViewCell () <UIScrollViewDelegate>

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
- .....
- }
- 
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
- {
- .....
- }

子类遵循了tableview的数据源和代理协议

@interface TPAskanswerSinglePageView () <UITableViewDataSource, UITableViewDelegate>

实现代理方法

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([UITableViewCell class])];
    NSString *content = self.dataArray[indexPath.row];
    cell.textLabel.text = content;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

这样的话,代理方法都是能够正常走到的。

经过测试还发现
在子类中设置代理对象也是可以的
子类中:

@implementation TPAskanswerSinglePageView

@synthesize refreshIndicatorView = _refreshIndicatorView;
@synthesize loadMoreIndicatorView = _loadMoreIndicatorView;

- (void)setUpUI
{
    self.tableView.delegate = self;
    self.tableView.dataSource = self;

父类子类的代理方法也都是能够正常走到的

由此可知,如果设置代理的时候
我们是可以将不同的代理方法在分别在子类或者父类里实现的

又经过测试发现
同一个代理协议的不同方法也是可以分别在子类和父类中实现的
如子类实现了

  • (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
  • 代理方法,父类实现了
    • (void)scrollViewDidScroll:(UIScrollView *)scrollView
      方法,都是可以正常执行的
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值