简体   繁体   中英

NSView in NSCell

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?

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:]).

因此,感谢@puzzle答案和更多的答案,是将我的NSCell子类设置为InterfaceBuilder中的主单元,然后调用了该方法,正如他所说,在tableView:objectValueForTableColumn:row:我需要返回数据然后绘制它。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM