تغيير لون nstableViewCell
-
04-10-2019 - |
سؤال
كيف يمكنني تغيير لون الخلية في 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:...];
إذا كنت تريد أن تحتوي الأعمدة على مظاهر مختلفة ، أو إذا كانت جميع الأعمدة ليست nstextfieldcells ، فاستخدمها [tableColumn identifier]
إلى ، ER ، تحديد العمود. يمكنك تعيين المعرف في Builder Interface عن طريق تحديد عمود الجدول.
// تم اختباره - حل 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;
}