更改NSTableViewCell的颜色

Mat*_* S. 10 cocoa background-color nstableviewcell

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

Ada*_*ble 14

在你的NSTableViewDelegatefor NSTableView,实现这个方法:

- (void)tableView:(NSTableView *)tableView 
  willDisplayCell:(id)cell 
   forTableColumn:(NSTableColumn *)tableColumn 
              row:(NSInteger)row
Run Code Online (Sandbox Code Playgroud)

NSTableView显示每个单元,这样就可以影响它的外观之前就其委托调用此.假设您正在使用NSTextFieldCells,对于要更改呼叫的单元格:

[cell setBackgroundColor:...];
Run Code Online (Sandbox Code Playgroud)

或者,如果要更改文本颜色:

[cell setTextColor:...];
Run Code Online (Sandbox Code Playgroud)

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

  • 确保在NSTextFieldCell上启用了`drawBackground`,否则这将无效! (11认同)

Ale*_*ers 0

尝试使用自定义NSView的 orNSTableView方法-setBackgroundColor:

  • NSTableView setBackgroundColor 对于设置 NSTableViewCell 的颜色没有任何作用。 (6认同)