Question

I've read a lot about this but i can't get it to work, i have a custom NSCell with this code

#import "ServiceTableCell.h"
@implementation ServiceTableCell

-(void)drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView {
    NSLog(@"I'm being called");
    NSView *newview = [[NSView alloc] initWithFrame:cellFrame];
    NSImage *image = [NSImage imageNamed:@"bg.png"];
    NSRect imagesize;
    NSImageView *IMV = [[NSImageView alloc] initWithFrame:imagesize];
    [IMV setImage:image];
    [newview addSubview:IMV];
    [controlView addSubview:newview];
}

And this my NSTableView data source:

- (long)numberOfRowsInTableView:(NSTableView *)tableView {
    return 3;
}

- (id)tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)tableColumn row:(long)row
{
    return [[ServiceTableCell alloc] initTextCell:@"dd"];
}

As i understand, the drawwithframe... gets called when the cell is initialized but it never gets called, so, what am I missing?

Was it helpful?

Solution

The method tableView:objectValueForTableColumn:row: should return an object value, not a cell.

Note that NSTableView is substantially different from UITableView, which you may be familiar with. For example, the data source doesn't return cells that are filled with the data, but returns the data. And the cell type in an NSTableView is set per column, you can't have a different kinds of cells in one column (well, technically, that's not entirely true, you could have different cells through -[NSTableColumn dataCellForRow:]).

OTHER TIPS

So thanks to @puzzle answer and a little more digging the answer was to set my subclass of NSCell as the main cell in the InterfaceBuilder, then the method was being called, and as he said, in tableView:objectValueForTableColumn:row: i needed to return the data to then draw it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top