Coloring a Row in an NSTableView
What I am 开发者_如何学JAVAlooking to do is set the background color of the selected row in an NSTableView when a I button is clicked.
I've seen other cases where people have used tableView:willDisplayCell:forTableColumn:row:
and setBackgroundColor:
but I don't think that will work in my situation where I want it to happen when a button is clicked.
I know that I can find the selected row with NSTableView's selectedRow
method and set the Background color for a cell with setBackgroundColor:
, but what I don't know how to do is get from a NSInteger for the selected row to an NSCell to set the background color of.
NSTableView
uses only one instance of NSCell
for each column. When drawing the contents, the cell is updated for each row. That’s why there’s no method to get a cell for a specified row—you have to modify the cell in tableView:willDisplayCell:forTableColumn:row:
.
You can tell the table view to update only one row using reloadDataForRowIndexes:columnIndexes:
.
To set background color to NSTableview
Row
- (void)tableView:(NSTableView *)tableView willDisplayCell:(id)cell1 forTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row
{
if(row==1)
[cell1 setBackgroundColor:[NSColor redColor]];
else if(row==2||row==3)
[cell1 setBackgroundColor:[NSColor greenColor]];
else
[cell1 setBackgroundColor:[NSColor clearColor]];
}
using this method we can give different color to each row..
Make sure drawsBackground
is enabled on the NSTextFieldCell
otherwise this will have no effect!
If you simply want to change the color of an entire selected row:
NSInteger selectedRow = [self.nsTableView selectedRow];
NSTableRowView* rowView = [self.nsTableView rowViewAtRow:selectedRow makeIfNecessary:NO];
[rowView setBackgroundColor:[NSColor blackColor]];
glhf
精彩评论