NstableView更改一行的文本颜色
-
24-10-2019 - |
题
我需要更改我的nstable视图1-更改颜色:选择的颜色和文本颜色2-更改文本颜色,每行都取决于某些输入参数,
对于更改每一行的文本彩色,我应该覆盖委托方法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]];
}
}
使用上述代码,它将在日志中例外,我可以看到该行 - [nscell setTextColor:]:未识别为实例的未识别的选择器看起来像我需要设置文本字段单元格的地方,但是如何和何处我不知道,请帮助我
另一件事是,最初我不需要任何单元的背景,但是一旦选择了单元格,我也可能需要更改背景,或者您可以说突出显示颜色,我也可以在Willdisplaycell中获得相同的功能
解决方案
自从我这样做以来已经有一段时间了,但是当我需要这样做时,我总是参考Corbin Dunn的这篇博客文章: 可可:NstableView的WillDisPlayCell委托法,[NSCELL SetTextColor]和“源列表”
顺便说一句,Corbin在Apple工作,据我了解,这是NstableView的原因。当他写有关可可的东西时,我总是一定要为其添加书签。
不隶属于 StackOverflow