有关某种原因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