sam*_*sam 5 cocoa nstextview nstableview
我有一个基于视图的单列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];
}
Run Code Online (Sandbox Code Playgroud)
我正在重用NSTextView的标准委托......
- (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;
}
Run Code Online (Sandbox Code Playgroud)
在标题中......
@protocol MyTextViewDelegate <NSTextViewDelegate>
- (void)didClickMyTextView:(id)sender;
@end
Run Code Online (Sandbox Code Playgroud)
在委托中,我实现了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];
}
Run Code Online (Sandbox Code Playgroud)