clickedRow for NSTableView

The meaning of clickedRow is more subtle than I thought. In my previous post, I mentioned a case for which right-clicking or control-clicking is involved. In this post I would like to talk about left-clicking.

On NSTableView, a row is selected when a mouse button is clicked and released, or mouse down and mouse up in more programmer friendly term. When the mouse button is “mouse downed”, it is “clicked”. However, the NSTableView has drag&drop capability. So, when a mouse button is down, it captures following mouse event. Probably due to that, “clickedRow” method of NSTableView doesn’t return an index of a clicked row. Instead it returns -1. So,  you can’t use the clickedRow.

By the way, why do we need to use it? For left-clicking case, you can click and select one row, and by clicking on other row, or “mouse down”, you can start dragging. However, although you may intend to drag the second row, what the NSTableView tracks is actually the first row. It doesn’t highlight the second row visually and in data-wise. This will confuse users. So, you should make it to track the second row.

How to solve this problem? The mouseDown method of NSView, which is a super class of the NSTableView should be overriden.

- (void) mouseDown:(NSEvent *)theEvent
	NSPoint event_location = [theEvent locationInWindow];
	NSPoint local_point = [self convertPoint:event_location toView:nil];
	JALog(@"mouseDown!!! at {%f, %f}", local_point.x, local_point.y);
	JALog(@"\t which is converted from window coordinate {%f, %f}", event_location.x, event_location.y );
	mClickedRow = [self rowAtPoint:local_point];
	JALog(@"rowInPoint = %d", mClickedRow );
	[super mouseDown:theEvent];

#pragma mark -
#pragma mark Setter & Getter
- (int) clickedRowFromMouseDown
	return mClickedRow;

Try calling clickedRowFromMouseDown instead of clickedRow. Then you can confirm that it works like below.



Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: