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: Sun, 22 Jun 2003 00:35:48 -0400
On Saturday, June 21, 2003, at 09:28 PM, Aaron Burghardt wrote:
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
Oops, I overlooked calling delegates. I had to call:
[[self delegate] tableView:self
willDisplayCell:cell
forTableColumn:[self
tableColumnWithIdentifier:@"label"]
row:(int)row];
Which I added right before the [cell highlight:YES...] call.
----
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.