Search code examples
objective-ccocoadelegatesnsoutlineview

How to call super class delegate method from subclass delegate method in Cocoa?


There is example class:

@interface OutlineViewController : NSOutlineView <NSOutlineViewDataSource, NSOutlineViewDelegate>
@end

@implementation OutlineViewController
- (NSView *)outlineView:(NSOutlineView *)outlineView viewForTableColumn:(NSTableColumn *)tableColumn item:(id)item
{
    NSTableCellView *result = nil;
if (myCondition)
{
// ...
    return result;
} else {
// how return defult?
}
}

@end

Is there possibility call default realization from delegate method?


Solution

  • Just use the super keyword to refer to the parent class as you've done in your comment:

    if (myCondition) {
        //...your implementation here
    }
    else {
        return [super outlineView:outlineview heightOfRowByItem:item];
    }
    

    For extra points, you might use -respondsToSelector: to check that super responds to the method in question.

    Update: I just noticed that the superclass in this case is NSOutlineView itself. This is quite confusing -- views and view controllers are different things, so calling something descended from a view a "view controller" is not a good plan. Also, note that the docs advise that "Subclassing NSOutlineView is not recommended."

    Nevertheless, I think I understand your question better now -- I think that by "default realization" you mean not the inherited version of the delegate method, but the behavior that the outline view would use if the delegate method weren't implemented at all. In this case, the answer is pretty simple: you can simply do what NSOutlineView itself would do. The documentation for -outlineView:heightOfRowByItem: says:

    Implement this method to support an outline view with varying row heights.

    For fixed row heights, on the other hand, NSOutlineView almost certainly uses the rowHeight property that it inherits from NSTableView. So, you can simply return rowHeight in those cases when you don't want to change the row height:

    if (myCondition) {
        //...your implementation here
    }
    else {
        return outlineView.rowHeight;
    }