xcode NSTableView 更改一行的文本颜色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4792667/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 20:08:23  来源:igfitidea点击:

NSTableView Changing Text Color for a row

objective-ccocoaxcodenstableviewnstableviewcell

提问by Amitg2k12

I need to change following properties for my NSTable View 1 -- Change Color:Row Color and Text Color when its selected 2 -- Change the Text Color , for each row it depends upon some input parameter,

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

For changing textcolor for each row, i should override delegate method willDisplayCell, This is what i have done , till now,

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

-- Creating the table ----

-- 创建表 ----

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]

;

--- Other delegate Method -------------

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

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

    }

}

---- Now this is how i am setting the text color ---

---- 现在这就是我设置文本颜色的方式 ---

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

}

With the above code, its going to exception in the Log, i could see the line -[NSCell setTextColor:]: unrecognized selector sent to instance Looks like somewhere i need to set the text Field cell, but how and where i have no idea, kindly help me,

使用上面的代码,它会在日志中出现异常,我可以看到行 -[NSCell setTextColor:]: unrecognized selector sent to instance 看起来像我需要设置文本字段单元格的地方,但我不知道如何以及在哪里,请帮助我,

Another thing is, Initially i don't need any background for cell, but once when cell is selected , then also i might need to change the Background or you can say highlight color, can i get the same in WillDIsplayCell too

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

回答by sosborn

It has been a while since I have done this but I always refer to this blog post by Corbin Dunn when I need to do it: Cocoa: willDisplayCell delegate method of NSTableView, [NSCell setTextColor], and “source lists”

我已经有一段时间没有这样做了,但是当我需要这样做时,我总是参考 Corbin Dunn 的这篇博文:Cocoa:NSTableView 的 willDisplayCell 委托方法、[NSCell setTextColor] 和“源列表”

By the way, Corbin works at Apple and from what I understand is responsible for NSTableView. When he blogs about anything Cocoa I always be sure to bookmark it.

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