I\'m writting a subclass of UITableView and I want my subclass to handle some of the UITableViewDelegate methods itself before passing them along to the \"real\" delegate as wel
For performance, UITableView
checks and remembers which delegate methods are available as soon as the delegate is set. You set the delegate self
first, then the trueDelegate
. So at the time the delegate is set on the UITableView
, trueDelegate
is nil
, and so -respondsToSelector:
on that one always returns NO
.
To fix that, set the delegate after trueDelegate
is set. Also, you can simplify the forwarding code. Remove all the code you have above except for the property and replace it with:
- (void)setDelegate:(id )delegate
{
if (delegate == self) return;
self.trueDelegate = delegate;
[super setDelegate:self];
}
- (BOOL)respondsToSelector:(SEL)aSelector
{
if ([super respondsToSelector:aSelector]) return YES;
return [self.trueDelegate respondsToSelector:aSelector];
}
- (id)forwardingTargetForSelector:(SEL)aSelector
{
return self.trueDelegate;
}