由於某種原因,我的表視圖的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];
}
從控制檯resut說:
THE TAG: 0
-[NSTableView title]: unrecognized selector sent to instance 0x100132480
乍一看(至少對我來說)這應該說是標籤100,但它不。 另外(從第二個控制檯輸出可以看出),看起來發送到「buttonClick」選擇器的參數是不正確的,我認爲它應該接收NSButtonCell,但它正在接收NSTableView。
謝謝,這幫了很多 – 2010-03-24 19:58:30