Corbin's Treehouse - Corbin Dunn, Santa Cruz, CA
Plug Bug
Treehouse
Photography
Videos
Shop
Unicycling
About

Different cells on each row in an NSTableView or NSOutlineView


Some people have asked me how to dynamically change the cell that is displayed for each row in an NSTableView or NSOutlineView. Generally, the same cell is used for each row, but it is possible to use a different cell for each row, if you like. NSTableColumn can change the cell that is used for each row. NSTableView calls -[NSTableColumn dataCellForRow:(int)row] for each row. Now, consider this code:

@interface NSObject (VariableCellColumnDelegate)
- (id)tableColumn:(NSTableColumn *)column inTableView:(NSTableView *)tableView dataCellForRow:(int)row;
@end

@interface VariableCellColumn : NSTableColumn
@end

@implementation VariableCellColumn

- (id)dataCellForRow:(int)row {
    id delegate = [[self tableView] delegate];
    if ([delegate respondsToSelector:@selector(tableColumn:inTableView:dataCellForRow:)]) {
        return [delegate tableColumn:self inTableView:[self tableView] dataCellForRow:row];
    } else {
        return [super dataCellForRow:row];
    }
}
@end

If the delegate simply implements the method:

- (id)tableColumn:(NSTableColumn *)column inTableView:(NSTableView *)tableView dataCellForRow:(int)row;

…you will be able to dynamically change the cells, with very little effort! In IB, you can set the class for the NSTableColumn to be VariableCellColumn (after dragging the header into IB), and you won’t have to do any work of dynamically creating NSTableColumn’s.



One Response to “Different cells on each row in an NSTableView or NSOutlineView”

  1. soeren says » Blog Archive » Hero of the Day says:

    [...] Podcast 9 with video Hero of the Day Today, mine gotta be Corbin. Thank you, thank you, thank you. This entry was written by Sören ‘chuck [...]

Leave a Reply

Line and paragraph breaks automatic.
XHTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

(c) 2008-2012 Corbin Dunn

Corbin's Treehouse is powered by WordPress. Made on a Mac.

Subscribe to RSS feeds for entries and comments.

17 queries. 0.287 seconds.