NSTableView更改行的文本颜色

时间:2011-01-25 11:01:17

标签: objective-c cocoa xcode 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]

;

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

- (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";

    }

}

----现在我正在设置文字颜色---

- (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]];
    }

}

使用上面的代码,它会在Log中出现异常,我可以看到该行 - [NSCell setTextColor:]:发送到实例的无法识别的选择器 看起来像我需要设置文本字段单元格,但我不明白的方式和地点, 请帮助我,

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

1 个答案:

答案 0 :(得分:7)

我已经做了一段时间了但是我总是在我需要的时候参考Corbin Dunn撰写的这篇博文:Cocoa: willDisplayCell delegate method of NSTableView, [NSCell setTextColor], and “source lists”

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

相关问题