Re: NSTableView, PopUpButtons, and controlling selection
Re: NSTableView, PopUpButtons, and controlling selection
- Subject: Re: NSTableView, PopUpButtons, and controlling selection
- From: Aaron Burghardt <email@hidden>
- Date: Sat, 21 Jun 2003 21:28:39 -0400
On Wednesday, June 18, 2003, at 11:12 AM, Aaron Burghardt wrote:
>
Hi All,
>
>
I am using an NSTableView in which one column has a PopUpButton cell.
>
I have the popup displaying and functioning correctly. However, I
>
don't want the selection in the table to change when the user clicks
>
on the popup, but I haven't figured out how to achieve this behavior
>
(the check boxes next to the song names in iTunes exhibit the behavior
>
I'm looking for).
>
>
I experimented with
>
>
- (BOOL)selectionShouldChangeInTableView:(NSTableView *)aTableView
>
>
but if I return NO, then the mouse click event doesn't reach the
>
popup. I also looked at subclassing NSPopUpButtonCell and tracking the
>
mouse in the cell, but that's too late, the selection has already
>
changed. I could use tracking in the cell and require a ctrl-click to
>
activate the popup, but I shouldn't have to resort to that.
>
>
The only other possibility I can think of is to subclass NSTableView
>
and override the mousedown event, but I suspect I will have to do a
>
lot of work to determine whether the event happened over popup cell.
>
>
OK, I've been experimenting with subclassing NSTableView to prevent the
selection from changing when a cell is clicked and have achieved
acceptable results. Here is my code:
@implementation CustomTableView
- (void)mouseDown:(NSEvent *)theEvent
{
NSPoint location = [self convertPoint:[theEvent locationInWindow]
fromView:nil];
if ( NSPointInRect(location, [self rectOfColumn:2]) ) {
int row = [self rowAtPoint:location];
NSPopUpButtonCell *cell = [[self
tableColumnWithIdentifier:@"popup"] dataCellForRow:row];
NSRect frame = [self frameOfCellAtColumn:2 row:row];
[cell highlight:YES withFrame:frame inView:self];
if ([cell trackMouse:theEvent inRect:frame ofView:self
untilMouseUp:NO]) {
[[self dataSource] tableView:self
setObjectValue:[NSNumber numberWithInt:[cell
indexOfSelectedItem]]
forTableColumn:[self
tableColumnWithIdentifier:@"popup"]
row:row];
}
[cell highlight:NO withFrame:frame inView:self];
} else {
[super mouseDown:theEvent];
}
}
@end
A couple of notes:
1. This same approach works with a checkbox also, probably other cells
too. Change NSPopUpButtonCell to NSButtonCell and [cell
indexOfSelectedItem] to [cell state].
2. Turning the highlight on and off seems to be critical for the cell
to update it's value and draw itself correctly, especially when it's a
checkbox.
3. Be sure to deselect the "Editable" property in Interface Builder for
the particular table column that has the alternate cell. If you don't,
the user can tab into the column from another column when editing.
4. If you use the technique above as-is with a checkbox, the checkbox
doesn't behave exactly as normal. If you click and hold the mouse
button on the checkbox, it highlights. Move the mouse off the checkbox
control and the highlight goes off. So far, this is normal. But if you
mouse back over the checkbox, it doesn't highlight again like normal.
There is an example that demonstrates how to do this in the Events
section of the developer documentation, but I haven't pursued it yet:
file:///Developer/Documentation/Cocoa/TasksAndConcepts/
ProgrammingTopics/BasicEventHandling/index.html
And a couple questions:
1. Is there a better solution than subclassing NSTableView? It seems
like overkill, but maybe I'm just being paranoid about subclassing.
2. I'm still pretty new with Cocoa, so I may have overlooked something
with regards to controls and cells. Is this the right approach to
dealing with a cell and does it look complete?
Thanks,
----
Aaron Burghardt
email@hidden
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.