我有一个基于 View 的单列NSTableView。在我的NSTableCellView子类中,我有一个NSTextView,它是可选的,但不可编辑。
当用户直接单击NSTableCellView时,该行将正确突出显示。但是,当用户单击该NSTableCellView内的NSTextView时,该行不会突出显示。
如何获得对NSTextView的单击以传递给NSTableCellView,以便突出显示行?
类层次结构如下所示:
NSScrollView> NSTableView> NSTableColumn> NSTableCellView> NSTextView
最佳答案
这就是我最终要做的。我做了一个NSTextView的子类并覆盖mouseDown:如下...
- (void)mouseDown:(NSEvent *)theEvent
{
// Notify delegate that this text view was clicked and then
// handled the click natively as well.
[[self myTextViewDelegate] didClickMyTextView:self];
[super mouseDown:theEvent];
}
我正在重用NSTextView的标准委托(delegate)...
- (id<MyTextViewDelegate>)myTextViewDelegate
{
// See the following for info on formal protocols:
// stackoverflow.com/questions/4635845/how-to-add-a-method-to-an-existing-protocol-in-cocoa
if ([self.delegate conformsToProtocol:@protocol(MyTextViewDelegate)]) {
return (id<MyTextViewDelegate>)self.delegate;
}
return nil;
}
在标题中...
@protocol MyTextViewDelegate <NSTextViewDelegate>
- (void)didClickMyTextView:(id)sender;
@end
在委托(delegate)中,我实现didClickMyTextView:来选择行。
- (void)didClickMyTextView:(id)sender
{
// User clicked a text view. Select its underlying row.
[self.tableView selectRowIndexes:[NSIndexSet indexSetWithIndex:[self.tableView rowForView:sender]] byExtendingSelection:NO];
}
关于cocoa - 单击NSTableCellView内的NSTextView时,如何在NSTableView中选择一行?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10184113/