Shared UITableViewDelegate

后端 未结 1 1433
无人共我
无人共我 2021-02-09 18:21

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

相关标签:
1条回答
  • 2021-02-09 18:55

    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 <UITableViewDelegate>)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;
    }
    
    0 讨论(0)
提交回复
热议问题