Question

I have multiple NSTextFields in my NSTableCellView: enter image description here

With a double-click action, I call [self.outlineView editColumn:0 row:clickedRow withEvent:nil select: YES]; which works in activating editing in the first text field. I have also setup nextKeyViews in IB so that user can press Tab key to tab through all the fields. But when I try to select the text fields directly with the mouse key press, it never works. It only selects/deselect editing on the NSTableCellView, and hence only edits the first text field each time.

How can I get this to work so that I can select and edit the correct field?

Was it helpful?

Solution

Found a solution:

  • subclass NSTableView / NSOutlineView
  • in subclass, override - (void) mouseDown:(NSEvent *)theEvent

In mouseDown:

        NSPoint selfPoint = [self convertPoint:theEvent.locationInWindow fromView:nil];
        NSInteger row = [self rowAtPoint:selfPoint];
        if (row>=0) [(ContactInfoTableCellViewMac *)[self viewAtColumn:0 row:row makeIfNecessary:NO]
                     mouseDownForTextFields:theEvent];

In ContactInfoTableCellViewMac:

- (void) mouseDownForTextFields:(NSEvent *)theEvent {
    if ((NSCommandKeyMask | NSShiftKeyMask) & [theEvent modifierFlags]) return;
    NSPoint selfPoint = [self convertPoint:theEvent.locationInWindow fromView:nil];
    for (NSView *subview in [self subviews])
        if ([subview isKindOfClass:[NSTextField class]])
            if (NSPointInRect(selfPoint, [subview frame]))
                [[self window] makeFirstResponder:subview];
}

Full reference: Respond to mouse events in text field in view-based table view

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top