I'm subclassing UITableView
and its protocol UITableViewDelegate
and its "datasource" UITableDataSource
, everything works fine but I haven't find the clean way to forward the methods from "delegate" and "datasource" from UITableView
.
I have tried to "play" with respondsToSelector:
, forwardingTargetForSelector:
and forwardInvocation:
methods of NSObject
but I didn't get anything.
I'll show you some code about I'm trying:
My CustomTableView.h
:
@protocol TableViewCustomDelegate <UITableViewDelegate>
- (void) anyCustomMethodDelegate;
@end
@protocol TableViewCustomDataSource <UITableViewDataSource>
- (NSInteger) anyCustomMethod;
@end
@interface TableViewCustom : UITableView <UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, weak) id<TableViewCustomDelegate> myDelegate;
@property (nonatomic, weak) id<TableViewCustomDataSource> myDataSource;
@end
And this is my TableViewCustom.m
:
@implementation TableViewCustom
-(BOOL)respondsToSelector:(SEL)aSelector {
if ([self.myDelegate respondsToSelector:aSelector]) {
return YES;
}
return [super respondsToSelector:aSelector];
}
-(id)forwardingTargetForSelector:(SEL)aSelector {
if ([self.myDelegate respondsToSelector:aSelector]) {
return self.myDelegate;
}
return [super forwardingTargetForSelector:aSelector];
}
...
...
- (void) setMyDelegate:(id<TableViewCustomDelegate>)delegate
{
[super setDelegate:self];
_myDelegate = delegate;
}
- (void) setMyDataSource:(id<TableViewCustomDataSource>)dataSource
{
[super setDataSource:self];
_myDataSource = dataSource;
}
..
..
// A method from UITableViewDelegate that I would like to avoid
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
if ([self.delegateDelegate
respondsToSelector:@selector(tableView:viewForHeaderInSection:)]) {
return [self.myDelegate tableView:tableView
viewForHeaderInSection:section];
}
return nil;
}
..
..
@end
I would like to avoid conforms methods just to forward it to "myDelegate".
I think it should be work with responsToSelector:
and forwardingTargetForSelector:
like I do in my code but it doesn't work. I don't know if I'm doing something wrong or if it is not possible.
Thanks in advance.
Regards.