NSTableView Cell中的Cocoa NSView

时间:2012-11-11 18:33:39

标签: cocoa nstableview nsview

我是cocoa的新手,我很沮丧,我花了将近半天的时间试图找出如何将NSView添加到NSTableView单元格,但我还没有找到一个可以帮助的好指南我做我想做的事,也许有人可以看看我尝试过的东西并告诉我为什么它不起作用以及我怎样才能让它起作用......

-(NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row{
NSTableCellView *view = [tableView makeViewWithIdentifier:@"MyView" owner:self];
NSTextField *textfield = [[NSTextField alloc]initWithFrame:NSMakeRect(0, 0, 100, 30)];
[textfield setStringValue:predictate_search[row]];
[textfield setBackgroundColor:[NSColor redColor]];
[view addSubview:textfield];
[view setNeedsDisplay:YES];
return view;
}

我想要实现的是在彼此之上有两个NSTextField,并且表格单元格具有自定义背景。以上是我只是试图让一个NSTextField工作,但没有运气......

正在以编程方式创建NSTableView:

NSScrollView *scrollView = [[NSScrollView alloc]initWithFrame:bg];
    [scrollView setHasVerticalScroller:YES];
    [self addSubview:scrollView];

    search_results = [[NSTableView alloc]initWithFrame:bg];
    NSTableColumn *column = [[NSTableColumn alloc] initWithIdentifier:@"id"];
    [[column headerCell] setStringValue:@"Cities"];
    [column setWidth:1000.0];

    [search_results addTableColumn:column];
    [search_results setDelegate:(id)self];
    [search_results setDataSource:(id)self];
    [search_results reloadData];

    [scrollView setDocumentView:search_results];

我对makeViewWithIdentifier:的内容感到有些困惑,我在NSTableViews上观看了WWDC 2011视频,但我仍然不太确定。

如果您需要更多信息,请询问

由于

修改 第一个回答后:

-(NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row{
NSTableCellView *view = [tableView makeViewWithIdentifier:[tableColumn identifier] owner:self];
if(view == nil){
    NSTableCellView *view = [[NSTableCellView alloc]initWithFrame:[tableView frame]];
    view.identifier = [tableColumn identifier];
}
NSTextField *textfield = [[NSTextField alloc]initWithFrame:NSMakeRect(0, 0, 100, 30)];
[textfield setStringValue:predictate_search[row]];
[textfield setBackgroundColor:[NSColor redColor]];
[view addSubview:textfield];
[view setNeedsDisplay:YES];
return view;

}

然而它仍然不起作用?

3 个答案:

答案 0 :(得分:7)

下面的代码解决了我的问题:

-(NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row{
NSView *view = [tableView makeViewWithIdentifier:@"MyView" owner:self];

if (view == nil) {
    view = [[NSView alloc]initWithFrame:NSMakeRect(0, 0, 100, 30)];
    NSTextField *result = [[NSTextField alloc] initWithFrame:NSMakeRect(0, 25, 800, 25)];
    NSTextField *result2 = [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, 800, 25)];
    result.stringValue = [predictate_search objectAtIndex:row];
    result2.stringValue = @"UnitedKingdom";
    [view addSubview:result];
    [view addSubview:result2];
    [view setNeedsDisplay:YES];
}

return view;

}

感谢您的帮助:)

答案 1 :(得分:1)

返回单元格的方法必须创建单元格,如果它不是可以出列的(因为没有单元格)

// There is no existing cell to reuse so we will create a new one
if (result == nil) {

     // create the new NSTextField with a frame of the {0,0} with the width of the table
     // note that the height of the frame is not really relevant, the row-height will modify the height
     // the new text field is then returned as an autoreleased object
     result = [[[NSTextField alloc] initWithFrame:...] autorelease];

     // the identifier of the NSTextField instance is set to MyView. This
     // allows it to be re-used
     result.identifier = @"MyView";
  }

答案 2 :(得分:1)

好吧,看看这个,我想补充一个答案,以防将来有人绊倒这个。

视图在代码片段的第二行中定义。如果它为零,则它将进入if语句并再次定义视图。 in语句中出现的视图会消失,因为它是在括号范围内定义的,当程序离开该范围时会死亡,而原始的nil就会消失。

现在可能会有更多问题,但这种问题很突出。我正在研究这个问题,发现了这个问题,并注意到了范围问题。