我的 NSTableCellView 中有多个 NSTextField:enter image description here

通过双击操作,我调用 [self.outlineView editColumn:0 row:clickedRow withEvent:nil select: YES]; 它可以激活第一个文本字段中的编辑。我还在 IB 中设置了 nextKeyViews,以便用户可以按 Tab 键来浏览所有字段。但是当我尝试直接用鼠标按键选择文本字段时,它永远不起作用。它仅选择/取消选择 NSTableCellView 上的编辑,因此每次仅编辑第一个文本字段。

我怎样才能让它工作,以便我可以选择和编辑正确的字段?

有帮助吗?

解决方案

找到了解决办法:

  • 子类 NSTableView / NSOutlineView
  • 在子类中,覆盖 - (void) mouseDown:(NSEvent *)theEvent

在鼠标按下时:

        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];

在 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];
}

完整参考: 响应基于视图的表格视图中文本字段中的鼠标事件

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top