NSButtonCell 操作的问题

发布于 2024-08-26 04:41:36 字数 1268 浏览 10 评论 0原文

由于某种原因,我的表视图的 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,但事实并非如此。 另外(从第二个控制台输出可以看出),发送到“buttonClick”选择器的参数似乎不正确,我相信它应该接收 NSButtonCell,但它正在接收 NSTableView。

For some reason the NSButtonCell for my table view is passing the wrong object as a parameter. I am trying to read the tag of the NSButtonCell after it is clicked.

Here is a simplified version of my code:

- (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 resut from the console says:

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

At first sight (for me at least) this should say that the tag is 100, but it doesn't.
Also (as it can be seen from the second console output), it appears that the parameter being sent to the "buttonClick" selector is incorrect, I believe it should be receiving the NSButtonCell, but it is receiving the NSTableView.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

守望孤独 2024-09-02 04:41:36

显然,发件人是您的表视图,但不是您的特定表视图单元格。

我不知道如何让表格单元格成为发送者,但是您可以通过查找单击的行和列的索引来知道单击了哪个单元格,然后您可以执行单击单元格后应该发生的操作。

- (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]);
}

Apparently the sender is your table view but not your specific table view cell.

I have no idea about how to let the table cell become the sender, but you can know which cell is clicked by looking for the index of the clicked row and column, and then you can do what ought to happen after the cell is clicked.

- (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]);
}
倒数 2024-09-02 04:41:36

在这种情况下,发送者确实是一个 NSTableView,但您只需使用 [sender clickedRow] 和 [sender clickedColumn] 即可检索实际触发事件的控件的行和列。

In this case the sender is indeed an NSTableView but you can retrieve the row and column of the control that actually triggered the event simply with [sender clickedRow] and [sender clickedColumn].

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文