I’ve subclassed the cells of a NSOutlineView, by setting the custom class in interface builder.
I’ve implemented this delegate method to configure the cells:
- (void)outlineView:(NSOutlineView *)outlineView willDisplayCell:(id)cell forTableColumn:(NSTableColumn *)tableColumn item:(id)item
Also, I’ve implemented this method in my custom cell class:
- (NSUInteger)hitTestForEvent:(NSEvent *)event inRect:(NSRect)cellFrame ofView:(NSView *)controlView
which is invoked twice every time I click on the cell. I’m wondering why not just once. The event type is always MouseDown.
I don’t know if this matters, but it is invoked twice even if the cell has not parents or children. So it can’t be the cells hierarchy.
If I can’t rely on hitTestForEvent to trigger an action when a specific area of my cell is clicked, which method should I use ?
Thanks
-hitTestForEvent:inRect:ofViewis entirely the wrong method to be using to trigger actions. You should be using-trackMouse:inRect:ofView:untilMouseUp:, or-startTrackingAt:inView:,-continueTracking:at:inView:and-stopTracking:at:inView:mouseIsUp:.Important Note: If you implement your own mouse tracking loop in
-trackMouse:inRect:ofView:untilMouseUp:, you should document this fact somewhere, because generally speaking it will preclude the use of the other three methods. Some of theNSCellsubclasses in the AppKit framework do this and fail to document that they have done so (with the result that you’ll ponder for hours why it is that-startTrackingAt:inView:never gets called).How do you implement your own tracking loop? Like this:
(The above code was just typed in here, so the usual caveats apply; it assumes the existence of an
NSRectcalledmyClickRectthat defines the active area of your cell. You might need to calculate that fromcellFrameat the head of the method.)Obviously you can watch for and handle other events too, if they are relevant to you.
Perhaps I should also add that the three method approach, while conceptually cleaner, tends to be quite a bit slower, which generally leads me to prefer overriding
-trackMouse:inRect:ofView:untilMouseUp:as shown above.