Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mouseover detection in NSTableView's NSCell?

I am wanting to change the text background color on a tableview's cell when it is hovered upon, similar to how AddressBook "highlights" the label of a contact's element when you mouseover the label names. However I cannot figure out how to accomplish...

detecting a mouseover on a particular NSCell and... After detecting the cell his hovered upon, highlighting the text in that cell (not highlighting the entire row as if the user selected that row)

As NSCell is not a subclass of NSView this seems to be a very difficult task.

Any example of this or explanation on how this might be done would be greatly appreciated.

Thanks!

like image 378
Patrick Avatar asked May 07 '10 06:05

Patrick


1 Answers

I actually got it working using another method. I got it from the example posted here... http://www.cocoadev.com/index.pl?NSTableViewRollover https://web.archive.org/web/20111013060111/http://cocoadev.com/index.pl?NSTableViewRollover

Instead of using NSCell's tracking mechanism, I am tracking mouseEntered/mouseExited and mouseMoved within my subclassed NSTableView.

  1. When the tableview awakeFromNib method is called, I create a trackingRect from the visible portion of the tableview

  2. I have a BOOL ivar that is set to YES when the mouse is within the tracking area(mouseEntered) and NO when it is not (mouseExited)

  3. Within the mouseMoved method, I determine the current row the mouse cursor is on and set it to an NSInteger ivar and then call the tableview's setNeedsDisplayInRect: passing the rect of the row that the mouse is on.

  4. I also override resetCursorRects to remove the old tracking rect and add a new one...this method is called when the tableview is scrolled upon so that it's tracking the latest visible rect.

  5. Finally in my tableview's delegate, I determine the selected row (by retrieving the row index from the NSInteger ivar of the table view and change the cell's text color (or anything you want) if the currently drawn cell matches the row the mouse cursor is on. All this is done in the delegate method: tableView:(NSTableView *)aTableView willDisplayCell:(id)aCell forTableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)rowIndex

I hope this helps others, as this was a bit tricky. It is also probably important to make sure that tableview is the firstResponder when the view loads, just makes things a bit more streamlined and cleaner.

Btw, is there a way to make a specific control in a view always be the firstResponder with nothing else possible as being the firstResponder? Even a method such as the iPhones... viewWillAppear method will help as I could set the first responder each time the view is visible...but i'm not aware of such a method on the Mac.

like image 67
Patrick Avatar answered Oct 14 '22 14:10

Patrick