nstableviewcellの色を変更します
-
04-10-2019 - |
質問
nstableviewのセルの色を変更するにはどうすればよいですか?
解決
カスタムを使用してみてください NSView
そのため、または NSTableView
's -setBackgroundColor:
方法。
他のヒント
あなたの中で NSTableViewDelegate
のために NSTableView
, 、この方法を実装してください。
- (void)tableView:(NSTableView *)tableView
willDisplayCell:(id)cell
forTableColumn:(NSTableColumn *)tableColumn
row:(NSInteger)row
NSTableView
各セルを表示する前に、これを代表者に呼び出して、その外観に影響を与えることができます。 nstextfieldcellsを使用していると仮定すると、セルの場合は通話を変更したいと考えています。
[cell setBackgroundColor:...];
または、テキストの色を変更したい場合:
[cell setTextColor:...];
列を異なる外観にしたい場合、またはすべての列がnStextFieldCellsでない場合は、使用してください [tableColumn identifier]
に、列を識別します。テーブル列を選択して、インターフェイスビルダーに識別子を設定できます。
//テスト済み-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
}
//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;
}
所属していません StackOverflow