【问题标题】:Change color of NSTableViewCell更改 NSTableViewCell 的颜色
【发布时间】:2011-02-22 04:14:53
【问题描述】:

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

【问题讨论】:

    标签: cocoa background-color nstableviewcell


    【解决方案1】:

    在您的NSTableViewDelegate 中为NSTableView,实现此方法:

    - (void)tableView:(NSTableView *)tableView 
      willDisplayCell:(id)cell 
       forTableColumn:(NSTableColumn *)tableColumn 
                  row:(NSInteger)row
    

    NSTableView 在显示每个单元格之前在其委托上调用它,以便您可以影响其外观。假设您正在使用 NSTextFieldCells,对于要更改的单元格调用:

    [cell setBackgroundColor:...];
    

    或者,如果你想改变文本颜色:

    [cell setTextColor:...];
    

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

    【讨论】:

    • 确保 drawsBackground 在 NSTextFieldCell 上启用,否则这将无效!
    • 这仅适用于基于 NSCell 的表,不适用于 NSView @MiguelCarvajal
    【解决方案2】:

    // 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
        }
    

    【讨论】:

    • 我正在尝试使用此方法更改cellView.textField?.backgroundColor = NSColor.yellow 的背景颜色,但它不起作用。我将如何更改您的答案以使其适用于背景颜色?我还尝试将 backgroundFilter 应用于 cellView 本身 ​​cellView.backgroundFilters.append(highlightFilter) 但这只会将文本颜色更改为黄色。我的过滤器设置为let highlightFilter = CIFilter(name: "CIFalseColor")! 然后let highlightCIColor = CIColor(red: 0.9608, green: 0.9765, blue: 0, alpha: 1.0)(续)...
    • 最后在viewDidLoad() 中设置了过滤器highlightFilter.setValue(highlightCIColor, forKey: "inputColor1")。但正如我在之前的评论中所写的那样,这只会使文本颜色变为黄色,而不是 cellView 的背景。我希望能够更改单个单元格背景。正如预期的那样,更改 textColor 可以正常工作。
    • 这在技术上是错误的,因为它适用于基于视图的表格,而不是基于单元格的!
    【解决方案3】:

    尝试为此使用自定义NSView,或NSTableView-setBackgroundColor: 方法。

    【讨论】:

    • NSTableView setBackgroundColor 在设置 NSTableViewCell 的颜色方面没有任何作用。
    • 这是一个完全错误的答案。它没有回答最初的问题。
    【解决方案4】:
    //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;
    }
    

    【讨论】:

      猜你喜欢
      • 2016-05-29
      • 2023-03-12
      • 1970-01-01
      • 1970-01-01
      • 2015-05-14
      • 2013-10-03
      • 2020-09-14
      • 2017-11-15
      • 1970-01-01
      相关资源
      最近更新 更多