2011-02-28 66 views
3

喜我使用一個NSTableView它包含2列 甲乙變化圖像

A包含數據 B包含按鈕

我想當在內側的任何按鈕用戶點擊表視圖然後它的圖像必須被改變。

我如何添加tableview中單元格中的自定義按鈕:

NSButtonCell *buttonCell = [[[NSButtonCell alloc] init] autorelease]; 
    [buttonCell setBordered:NO]; 
    [buttonCell setImagePosition:NSImageOnly]; 
    [buttonCell setButtonType:NSMomentaryChangeButton]; 
    [buttonCell setImage:[NSImage imageNamed:@"uncheck.png"]]; 
    [buttonCell setSelectable:TRUE]; 
    [buttonCell setTarget:self]; 
    [buttonCell setAction:@selector(deleteSongContent:)]; 


    [[myTable tableColumnWithIdentifier:@"EditIdentifier"] setDataCell:buttonCell]; 

當我點擊該按鈕選擇方法被炒魷魚,但我不知道如何改變按鈕單元區域的圖像。

任何建議請!!!!!!!

編輯:

當我點擊按鈕的方法被調用,它是如何工作的:

-(void)selectButtonsForDeletion:(NSTableView *)tableView 
{ 
    NSEvent *currentEvent = [[tableView window] currentEvent]; 
    int columnIndex = [tableView columnAtPoint: 
        [tableView convertPoint: 
         [currentEvent locationInWindow] fromView:nil]]; 
    NSTableColumn *column = [[tableView tableColumns]objectAtIndex:columnIndex]; 
    NSButtonCell *aCell = [[tableView tableColumnWithIdentifier: 
          [column identifier]] 
          dataCellForRow:[tableView selectedRow]]; 


    NSInteger index = [[aCell title] intValue]; 
    if(![selectedIndexesArray containsObject:[NSNumber numberWithInt:index]]) 
    { 
     [aCell setImage:[NSImage imageNamed:@"check.png"]]; 
     [selectedIndexesArray addObject:[NSNumber numberWithInt:index]]; 
    } 
    else 
    { 
     [aCell setImage:[NSImage imageNamed:@"uncheck.png"]]; 
     [selectedIndexesArray removeObjectAtIndex:[selectedIndexesArray indexOfObject:[NSNumber numberWithInt:index]]]; 
    } 
} 
+0

由於NSButtonCell繼承自NSCell,你不能用圖像初始化單元嗎? ' - (id)initImageCell:(NSImage *)anImage' – dianovich 2011-02-28 12:03:07

+0

我有一些工作要做的按鈕點擊,所以我用NSButtonCell – Swati 2011-02-28 12:10:38

+0

我已編輯我的問題:該方法被解僱bt我無法獲得完美選定的按鈕單元格改變圖像 – Swati 2011-03-01 05:38:20

回答

2

最後我得到了答案:

我發現下面後提到的方法:

- (void)tableView: setObjectValue: forTableColumn: row: 

這種方法被稱爲:

-(void)tableView: willDisplayCell: forTableColumn: row: 

和我應用我的邏輯,如果檢查圖像已經應用在按鈕上,則不要更改它。

1

如果你想要的是改變對NSButtonCell的圖像,那麼你可以很容易做到這在正在調用的選擇器中。選擇器觸發接受一個參數。該參數是NSButtonCell對象(如果我是正確的)。

那麼,爲什麼不這樣做:

- (void) deleteSongContent:(NSButtonCell*) button 
{ 
    [button setImage[NSImage imageNamed:@"checked.png"]]; 
} 
+0

嘿,我正在做同樣的事情,但圖像沒有改變。我也試過這方法shouldSelectRow,但它應用圖像的所有按鈕,即使是那些沒有被選中..... – Swati 2011-02-28 13:39:02

+0

嗯,當你把一個斷點deleteSongContent方法會發生什麼?變量「按鈕」是否存在? – 2011-02-28 18:07:31

+0

請參閱我編輯的帖子及與其相關的評論... – Swati 2011-03-01 05:39:07