Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Cocoa 更改NSTableViewCell的颜色_Cocoa_Background Color_Nstableviewcell - Fatal编程技术网

Cocoa 更改NSTableViewCell的颜色

Cocoa 更改NSTableViewCell的颜色,cocoa,background-color,nstableviewcell,Cocoa,Background Color,Nstableviewcell,如何更改NSTableView中单元格的颜色 尝试使用自定义的NSView或NSTableView的-setBackgroundColor:方法。在NSTableViewDelegate中,为NSTableView实现以下方法: - (void)tableView:(NSTableView *)tableView willDisplayCell:(id)cell forTableColumn:(NSTableColumn *)tableColumn r

如何更改NSTableView中单元格的颜色

尝试使用自定义的
NSView
NSTableView
-setBackgroundColor:
方法。在
NSTableViewDelegate
中,为
NSTableView
实现以下方法:

- (void)tableView:(NSTableView *)tableView 
  willDisplayCell:(id)cell 
   forTableColumn:(NSTableColumn *)tableColumn 
              row:(NSInteger)row
NSTableView
在显示每个单元格之前对其委托调用此函数,以便您可以影响其外观。假设您使用的是NSTextFieldCells,对于要更改的单元格,请调用:

[cell setBackgroundColor:...];
或者,如果要更改文本颜色:

[cell setTextColor:...];

如果希望列具有不同的外观,或者如果所有列都不是NSTEXTFIELDCELL,请使用
[tableColumn identifier]
来识别列。您可以通过选择表格列在Interface Builder中设置标识符。

/TESTED-Swift 3解决方案…用于更改单个列中单元格文本的颜色。“我的tableview”中的所有列都具有唯一标识符

    func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? {

        let myCell:NSTableCellView = tableView.make(withIdentifier: (tableColumn?.identifier)!, owner: self) as! NSTableCellView
        if tableColumn?.identifier == "MyColumn" {
            let results = arrayController.arrangedObjects as! [ProjectData]
            let result = results[row]
            if result.ebit < 0.0 {
                myCell.textField?.textColor = NSColor.red
            } else {
                myCell.textField?.textColor = NSColor.black
            }
        }
        return myCell
    }
func tableView(tableView:NSTableView,viewFor tableColumn:NSTableColumn?,行:Int)->NSView?{
让myCell:NSTableCellView=tableView.make(带有标识符:(tableColumn?.identifier)!,所有者:self)作为!NSTableCellView
如果tableColumn?.identifier==“MyColumn”{
让结果=arrayController.arrangedObjects为![ProjectData]
让结果=结果[行]
如果结果显示息税前利润<0.0{
myCell.textField?.textColor=NSColor.red
}否则{
myCell.textField?.textColor=NSColor.black
}
}
返回迈塞尔
}
//用于使用Objective C的基于视图的表视图
//在NSTableViewDelegate中,实现以下操作
//此自定义设置使列编号为红色(如果为负数)。
-(NSView*)tableView:(NSTableView*)inTableView
viewForTableColumn:(NSTableColumn*)tableColumn
行:(NSInteger)行
{
NSTableCellView*结果=零;
如果([tableColumn.title isEqualToString:@“金额”]){
//选择以下方法之一来识别NSTableCellView
//在.xib文件创建中,将标识符保留为“空白”(默认值)
结果=[inTableView makeViewWithIdentifier:[tableColumn identifier]所有者:self];
//或将金额列的NSTableCellView标识符设置为“金额”
结果=[inTableView makeViewWithIdentifier:@“金额”所有者:self];
id aRecord=[[arrayController arrangedObjects]对象索引:行];
//测试相关字段的值
如果(记录量<0.0)
[[result textField]setTextColor:[NSColor COLOR WITHRGBRED:1.0绿色:0.0蓝色:0.0阿尔法:1.0];
}否则{
//允许默认值处理其余列
结果=[inTableView makeViewWithIdentifier:[tableColumn identifier]所有者:self];
}
返回结果;
}

确保在NSTextFieldCell上启用了
drawsBackground
,否则将无效!这只适用于基于NSCell的表,而不适用于NSView one@MiguelCarvajalNSTableView setBackgroundColor在设置NSTableViewCell的颜色方面没有任何作用。这是一个完全错误的答案。它没有回答原来的问题。我正在尝试使用此方法更改
cellView.textField?.backgroundColor=NSColor.yellow
的背景色,但它不起作用。我将如何更改您的答案以使其适用于背景色?我还尝试将backgroundFilter应用到cellView本身
cellView.backgroundFilters.append(highlightFilter)
,但这只会将文本颜色更改为黄色。我的过滤器设置为
let highlightFilter=CIFilter(名称:“CIFalseColor”)
然后让highlightCIColor=CIColor(红色:0.9608,绿色:0.9765,蓝色:0,alpha:1.0)(续)…最后在
viewDidLoad()
中设置过滤器
highlightFilter.setValue(highlightCIColor,forKey:“inputColor1”)
。但正如我在之前的评论中所写,这只会将文本颜色变为黄色,而不是cellView的背景。我希望能够更改单个单元格的背景。正如预期的那样,更改textColor效果很好。
//for VIEW based TableViews using Objective C
//in your NSTableViewDelegate, implement the following
//this customization makes the column numbers red if negative.
- (NSView *)tableView:(NSTableView *)inTableView
   viewForTableColumn:(NSTableColumn *)tableColumn
                  row:(NSInteger)row
{
    NSTableCellView *result = nil;
    if ([tableColumn.title isEqualToString: @"Amount"]) {
        //pick one of the following methods to identify the NSTableCellView
        //in .xib file creation, leave identifier "blank" (default)
         result = [inTableView makeViewWithIdentifier:[tableColumn identifier] owner:self];
        //or set the Amount column's NSTableCellView's identifier to "Amount"
        result = [inTableView makeViewWithIdentifier:@"Amount" owner:self];
        id aRecord = [[arrayController arrangedObjects] objectAtIndex:row];
        //test the relevant field's value
        if ( aRecord.amount < 0.0 )
            [[result textField] setTextColor:[NSColor colorWithSRGBRed:1.0 green:0.0 blue:0.0 alpha:1.0]];
    } else {
        //allow the defaults to handle the rest of the columns
        result = [inTableView makeViewWithIdentifier:[tableColumn identifier] owner:self];
    }
    return result;
}