简体   繁体   English

NSCell中的NSView

[英]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 我已经读了很多关于此的内容,但是我无法使它正常工作,我有一个自定义的NSCell并带有以下代码

#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: 这是我的NSTableView数据源:

- (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? 据我了解,drawwithframe ...是在初始化单元格时调用的,但从未调用过,所以,我想念的是什么?

The method tableView:objectValueForTableColumn:row: should return an object value, not a cell. tableView:objectValueForTableColumn:row:方法应返回一个对象值,而不是单元格。

Note that NSTableView is substantially different from UITableView , which you may be familiar with. 请注意, NSTableView与您可能熟悉的UITableView有很大不同 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:]). 而且NSTableView中的单元格类型是按列设置的,因此在一列中不能有不同类型的单元格(从技术上讲,这并不完全正确,可以通过-[NSTableColumn dataCellForRow:]来具有不同的单元格)。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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