NSButtonCell 操作的问题
由于某种原因,我的表视图的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
显然,发件人是您的表视图,但不是您的特定表视图单元格。
我不知道如何让表格单元格成为发送者,但是您可以通过查找单击的行和列的索引来知道单击了哪个单元格,然后您可以执行单击单元格后应该发生的操作。
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.
在这种情况下,发送者确实是一个 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].