我在 NSOutlineView
内的自定义 View 内有文本字段。编辑其中一个单元格需要单击一下、暂停一下,然后再单击一次。第一次单击选择 TableView 行,第二次单击将光标绘制在字段中。双击单元格即可在基于单元格的表格 View 中进行编辑,但只能选择该行。
我想要的行为:一键更改选择并编辑。
我需要重写什么才能获得此行为?
我读过其他一些帖子:
NSTextField
flyweight pattern似乎不适用于基于 View 的表格 View ,其中单元格 View 都是从 Nib 实例化的。- 我尝试对
NSTextField
进行子类化,例如 this solution describes ,但我重写的mouseDown
方法没有被调用。调用重写的awakeFromNib
和viewWillDraw
(在 this post 中提到)。当然,如果我将文本字段放在 TableView 之外的某个位置,就会调用 mouseDown 。
相比之下,我的单元格 View 中的 NSSegmentedControl
无需先选择行即可更改其值。
这是根据已接受的响应改编的工作解决方案:
在大纲 View 子类中:
-(void)mouseDown:(NSEvent *)theEvent {
[super mouseDown:theEvent];
// Forward the click to the row's cell view
NSPoint selfPoint = [self convertPoint:theEvent.locationInWindow fromView:nil];
NSInteger row = [self rowAtPoint:selfPoint];
if (row>=0) [(CellViewSubclass *)[self viewAtColumn:0 row:row makeIfNecessary:NO]
mouseDownForTextFields:theEvent];
}
在表格单元格 View 子类中:
// Respond to clicks within text fields only, because other clicks will be duplicates of events passed to mouseDown
- (void)mouseDownForTextFields:(NSEvent *)theEvent {
// If shift or command are being held, we're selecting rows, so ignore
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];
}
最佳答案
遇到同样的问题。经过一番努力,当我为 Highlight 选择
IB 中表格 View 的选项!None
而不是默认的 Regular
(其他选项是 Source List
)时,它神奇地起作用了
另一个选择是 https://stackoverflow.com/a/13579469/804616 处的解决方案,与此相比,这似乎更具体,但有点hacky。
关于cocoa - 响应基于 View 的表格 View 中文本字段中的鼠标事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7101237/