NSTableView更改行的文本颜色

Ami*_*k12 5 xcode cocoa objective-c nstableview nstableviewcell

我需要为我的NSTable View 1更改以下属性 - 更改颜色:选择时的行颜色和文本颜色2 - 更改文本颜色,每行取决于某些输入参数,

为了改变每一行的textcolor,我应该覆盖委托方法willDisplayCell,这就是我所做的,直到现在,

- 创建表----

pMyTableView       = [[[CustomTableView alloc] initWithFrame:clipViewBounds] autorelease];


NSTableColumn*  firstColumn     = [[[NSTableColumn alloc] initWithIdentifier:@"firstColumn"] autorelease];

[firstColumn setWidth:35];

[pMyTableView  addTableColumn:firstColumn];

NSTableColumn*  secondColumn        = [[[NSTableColumn alloc] initWithIdentifier:@"secondColumn"] autorelease];

[secondColumn setWidth:180];

[pMyTableView  addTableColumn:secondColumn];
    [pMyTableView setRowHeight:30];

    [self SetContactTableDisplayAttribute];

[pMyTableView setDataSource:self];
[scrollView setDocumentView:pOnLineCTView];

    [pMyTableView setDelegate:self]

;
Run Code Online (Sandbox Code Playgroud)

---其他委托方法-------------

- (id) tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)rowIndex{
    if([pColName isEqualToString:@"secondColumn"]) 
    {
           // Here there is some logic , to get the proper string that i wanted to display
        return @"tempString";

    }

}
Run Code Online (Sandbox Code Playgroud)

----现在这就是我设置文字颜色的方法---

- (void)tableView:(NSTableView *)aTableView willDisplayCell:(id)aCell forTableColumn:(NSTableColumn *)aTableColumn row:(int)rowIndex {

    NSString *colName = [aTableColumn identifier];
    if([colName isEqualToString:@"secondColumn"]){
        NSTextFieldCell *pCell = aCell;
        [pCell setTextColor:[NSColor blueColor]];
    }

}
Run Code Online (Sandbox Code Playgroud)

使用上面的代码,它会在Log中出现异常,我可以看到行 - [NSCell setTextColor:]:无法识别的选择器发送到实例看起来像某个地方我需要设置文本字段单元格,但是如何以及在哪里我不知道,请帮助我,

另一件事是,最初我不需要任何单元格的背景,但是一旦选择了单元格,那么我也可能需要更改背景或者你可以说高亮颜色,我也可以在WillDIsplayCell中获得相同的颜色

sos*_*orn 7

我已经有一段时间了,但是当我需要时,我总是参考Corbin Dunn的博客文章:Cocoa:NSTableView的willDisplayCell委托方法,[NSCell setTextColor]和"source lists"

顺便说一句,Corbin在Apple工作,据我所知,负责NSTableView.当他博客关于Cocoa的任何内容时,我总是一定要将它加入书签.