5

ビューベースの単一列NSTableViewがあります。NSTableCellViewサブクラス内には、選択可能ですが編集できないNSTextViewがあります。

ユーザーがNSTableCellViewを直接クリックすると、行が適切に強調表示されます。ただし、ユーザーがそのNSTableCellView内のNSTextViewをクリックしても、行は強調表示されません。

NSTextViewをクリックしてNSTableCellViewに渡し、行が強調表示されるようにするにはどうすればよいですか?

クラス階層は次のようになります:NSScrollView> NSTableView> NSTableColumn> NSTableCellView> NSTextView

4

3 に答える 3

6

これが私がやったことです。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の標準デリゲートを再利用しています...

- (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

デリゲートでは、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];
}
于 2012-04-25T01:54:02.423 に答える
1

または、NSTextFieldを使用してから、

textfield.bezeled = NO;
textfield.drawsBackground = NO;
textfield.editable = NO;
textfield.selectable = YES;
[textfield setRefusesFirstResponder: YES];
于 2015-02-26T00:48:53.103 に答える
0

私がここで抱えていたのと本質的に同じ問題があると思います:パスイベント。受け入れられた答えを参照してください。

同じパターンに従って、NSTextViewをサブクラス化し、オーバーライド- (void)mouseUp:(NSEvent *)theEventして、イベントをsuperViewに渡します。これはtableViewであると想定しています。

- (void)mouseUp:(NSEvent *)theEvent {
    [superView mouseUp:theEvent]; 
}
于 2012-04-23T04:15:32.763 に答える