views:

211

answers:

1

What I am looking 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.

+3  A: 

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:.

Nikolai Ruhe
I've just tried this http://grab.by/1CoC but it colors every single row grey not just the one I reload.
Joshua
1. Don't use `int`. Use `NSInteger`. 2. Try doing what Nikolai said. (Note that it requires Snow Leopard.) 3. Make your `tableView:willDisplayCell:forTableColumn:row:` method check both whether the row is selected and whether the user has clicked the button.
Peter Hosey
Joshua
Of course, since an NSOutlineView is an NSTableView, you can do with it whatever an NSTableView can do. (Inheritance == "is a")
Nikolai Ruhe
Ok, this is what I've got now, http://grab.by/1Dp2, However I am unsure how to tell whether the user has clicked the button.
Joshua
Have a Boolean variable, initialize it to false (`NO`), then set it to `YES` when the user presses the button. Reset it to `NO` at an appropriate later time.
Peter Hosey
Also, `=` is the assignment operator, not the equality operator. You should turn on the warning for that in Xcode so that the compiler will complain any time you make this mistake.
Peter Hosey
Ok, here's what's new, http://grab.by/1Dvd, but it doesn't actually trigger the delegate method.
Joshua