質問

何らかの理由にNSButtonCell自分の表示は間違ったオブジェクトとしてパラメータとします。私を読むようにタグのNSButtonCell後でボタンをクリックします。

ここでは簡易版のコード:

- (int)numberOfRowsInTableView:(NSTableView *)aTableView {
    return 3;
}

- (void)tableView:(NSTableView *)aTableView willDisplayCell:(id)aCell forTableColumn:(NSTableColumn *)aTableColumn row:(int)rowIndex {
    [aCell setTitle:@"Hello"];
    [aCell setTag:100];
}

- (void)buttonClick:(id)sender {
    NSLog(@"THE TAG %d",[sender tag]);
    NSLog(@"THE TITLE: %@",[sender title]);
}

- (void)refreshColumns {
    for (int c = 0; c < 2; c++) {
        NSTableColumn *column = [[theTable tableColumns] objectAtIndex:(c)];

        NSButtonCell* cell = [[NSButtonCell alloc] init];
        [cell setBezelStyle:NSSmallSquareBezelStyle];
        [cell setLineBreakMode:NSLineBreakByTruncatingTail];
        [cell setTarget:self];
        [cell setAction:@selector(buttonClick:)];
        [column setDataCell:cell];
    }
}

- (void)awakeFromNib {
    [self refreshColumns];
}

のresutコンソールからは

    THE TAG:   0
    -[NSTableView title]: unrecognized selector sent to instance 0x100132480

最初のグサイト(私にとって少なくともこのとの合作とでも言うべきこのタグは100ですが、ださい。もともできるから見る次のコンソール出力されるパラメータが相手に送られている"buttonClick"セレクタが不正確であると思うのでのNSButtonCellでのNSTableView.

役に立ちましたか?

解決

どうやら、送信者があなたのテーブルビューではなく、あなたの特定のテーブルビューセルである。

私は表のセルが送信者になるようにする方法については考えているが、あなたがクリックされた行と列のインデックスを探すことにより、クリックされたセルを知ることができ、その後、あなたが何ができるかは、セルの後に起こるべきクリックされます。

- (void)buttonClick:(id)sender {
    NSEvent *event = [NSApp currentEvent];
    NSPoint pointInTable = [tableView convertPoint:[event locationInWindow] fromView:nil];
    NSUInteger row = [tableView rowAtPoint:pointInTable];
    NSTableColumn *column = [[tableView tableColumns] objectAtIndex:[tableView columnAtPoint:pointInTable]];
    NSLog(@"row:%d column:%@", row, [column description]);
}

他のヒント

この場合、送信者が実際にNSTableViewですが、実際には、[送信者clickedRow]で簡単にイベントをトリガし、[送信者clickedColumn]というコントロールの行と列を取得することができます。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top