由于某些原因,我的表视图的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。
发布于 2010-03-25 03:03:11
显然,发送者是您的表格视图,而不是您特定的表格视图单元格。
我不知道如何让表格单元格成为发送者,但您可以通过查找被单击的行和列的索引来知道哪个单元格被单击,然后您可以在单元格被单击后执行应该发生的操作。
- (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]);
}发布于 2012-01-28 07:41:23
在本例中,发送方确实是一个NSTableView,但您可以使用发送方clickedRow和发送方clickedColumn检索实际触发事件的控件的行和列。
https://stackoverflow.com/questions/2506175
复制相似问题