每行绘制NSTableView文本 [英] Coloring NSTableView Text per row
问题描述
我有一个NSTableView,显示我有一个对象数组。对于每个对象(行),我想改变显示的文本的颜色,取决于我对每个对象运行的函数的结果;
例如所有在另一个列表(或一些其他需求)中存在的表中的所有对象我想以绿色文本显示它们,并且不存在的对象显示为红色。
假设您有 NSTextFieldCell (对于其他单元格,设置文本颜色可能会变化),您可以通过实施 NSTableView 的委托方法。
首先,您必须为 NSTableView ,无论是在Interface Builder中还是在您的代码中。这可以是您的应用程序控制器。
然后,只需实现以下方法:
- (void)tableView:(NSTableView *)aTableView willDisplayCell:(id)aCell forTableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)rowIndex {
NSTextFieldCell * cell = aCell;
if(...){
[cell setTextColor:[NSColor greenColor]];
} else if(...){
[cell setTextColor:[NSColor redColor]];
} else {
[cell setTextColor:[NSColor blackColor]];
}
}
每次NSTableView绘制一个单元格时,有机会在绘制之前对其进行修改。
查看 NSTableViewDelegate 文档页面了解更多详情。
I have a NSTableView that is displaying an array of objects I have. For each of these objects (rows) I would like to change the color of the text displayed depending on the results of a function I run on each object;
So for example all the object in the table that exist in another list (or some other requirement) I want to display them in green text, and objects that don't exist display in red.
How would I go about doing this?
Assuming that you have NSTextFieldCell in your table (for other cells, setting text color may vary), you can achieve this by implementing a NSTableView's delegate method.
First, you have to define a delegate for the NSTableView, either in Interface Builder or in your code. This can be your application controller for example.
Then, just implement the following method:
- (void)tableView:(NSTableView *)aTableView willDisplayCell:(id)aCell forTableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)rowIndex {
NSTextFieldCell *cell = aCell;
if (...) {
[cell setTextColor:[NSColor greenColor]];
} else if (...) {
[cell setTextColor:[NSColor redColor]];
} else {
[cell setTextColor:[NSColor blackColor]];
}
}
Each time the NSTableView will draw a cell, you have the opportunity to modify it before it get drawn.
Check out the NSTableViewDelegate documentation page for more details.
这篇关于每行绘制NSTableView文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!