4

何らかの理由で、テーブルビューの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];
}

コンソールからの結果は次のように述べています。

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

一見すると(少なくとも私にとっては)、これはタグが100であると言うべきですが、そうではありません。また(2番目のコンソール出力からわかるように)、「buttonClick」セレクターに送信されているパラメーターが正しくないようです。NSButtonCellを受信して​​いるはずですが、NSTableViewを受信して​​います。

4

2 に答える 2

4

どうやら送信者はあなたのテーブルビューですが、あなたの特定のテーブルビューセルではありません。

テーブルセルを送信者にする方法はわかりませんが、クリックされた行と列のインデックスを探すことで、どのセルがクリックされたかを知ることができ、セルがクリックされた後に何が起こるかを知ることができます。

- (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]);
}
于 2010-03-24T19:03:10.797 に答える
4

この場合、送信者は実際にはNSTableViewですが、[senderclickedRow]と[senderclickedColumn]を使用するだけで、実際にイベントをトリガーしたコントロールの行と列を取得できます。

于 2012-01-27T23:41:22.857 に答える