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
方法,都是可以正常执行的
- (void)scrollViewDidScroll:(UIScrollView *)scrollView