2013-05-28 41 views
1

在我的應用程序中,我面臨一個非常奇怪的問題。我試圖解決它幾個小時,但無法找到到目前爲止的問題。iphone開發:更改tableviewcell上的按鈕圖像

那麼,我有一個自定義單元格的tableview。每個單元格我都有一個按鈕。它就像一個空盒子。按下按鈕時,我想更改該按鈕的相關單元格圖像。其實我成功地做到了這一點,但不知何故,其他一些單元格的按鈕圖像也在變化,我無法弄清楚原因。這裏是我的代碼:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
    { 
     static NSString *CellIdentifier = @"cell"; 
     CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
     if (cell == nil) 
     { 
      //create new cell 
      cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
     } 
     [cell.tickButton setTag:indexPath.row]; 
     [cell.tickButton addTarget:self action:@selector(buttonWasPressed:)forControlEvents:UIControlEventTouchUpInside]; 

//cell has 2 label so I filled them with the contents of my object array that part is working with no problem 
     cell.leftLabel.text = [NSString stringWithFormat:@"%@", [[contacts objectAtIndex:indexPath.row]name]]; 
     cell.rightLabel.text = [NSString stringWithFormat:@"%@", [[contacts objectAtIndex:indexPath.row]email]]; 

     return cell; 
    } 

在我buttonWasPressed方法,我只是做:

-(IBAction)buttonWasPressed:(id)sender 
{ 
    UIButton *button = (UIButton *)sender; 
    [button setImage:[UIImage imageNamed:@"ticck.jpeg"] forState:UIControlStateNormal]; 

} 

正如我所說的它的工作原理之前,還會改變其他一些按鈕圖像,並且還當我向下滾動,回到我看到一些按鈕圖像的初始位置發生了變化。

回答

1

問題是調用

[cell.tickButton addTarget:self action:@selector(buttonWasPressed:)forControlEvents:UIControlEventTouchUpInside]; 

方法。

您每次添加目標時,您的單元格被重用並從不刪除。所以你的按鈕有很多目標,動作方法分別被多次調用。它可能會導致內存泄漏。

只需在if子句中添加addTarget方法即可。

它會解決你的一個問題,這是你的方法多次調用。但是當單元格被重新使用時,該按鈕具有更改後的圖像。你需要重新設置它。

要做到這一點,最好的方法是將屬性添加到預先存儲在單元格中的聯繫人對象,並在觸發操作方法時設置值。然後在cellForRowAtIndexPath:方法中,您可以通過檢查該屬性來設置按鈕的圖像。

+0

很好的答案。我是一隻新的蜜蜂。請大家粘貼一些代碼片段。它會有幫助。謝謝 –

1

做這個

NSString *CellIdentifier = [NSString stringWithFormat:@"cell%d",indexPath.row]; 
+0

通過這種方式,單元格不會被重用,並且如果表視圖加載大量數據(圖像和其他東西),則會出現內存和性能問題。 – danypata

+0

@Durgaprasad但它沒有工作...當我使用它而不是'靜態NSString * CellIdentifier = @「cell」;'這行我有空tableview:/ – death7eater

+0

它不會發生。通過使用這條線你不會重複使用單元格。它在這樣的條件下工作。 – Durgaprasad

6

您可以訪問該小區,並且可以更改按鈕圖像爲:

-(IBAction)buttonWasPressed:(id)sender 
{ 

    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:your_row inSection:your_section]; 
    UITableViewCell *currentCell = [table cellForRowAtIndexPath:indexPath]; 
    UIButton *button = (UIButton *)[currentCell viewWithTag:(UIButton *)sender.tag]; 
    [button setImage:[UIImage imageNamed:@"ticck.jpeg"] forState:UIControlStateNormal]; 
} 

希望它可以幫助你。

0

要重新使用您的單元格,必須在出列後將其清理乾淨。在這種情況下,您應該先重置圖像:

[cell.tickButton setImage:nil forState:UIControlStateNormal]; 

將任何內容分配給您的單元格之前。

0

必須保存其被壓比試試這個

if(pressed[cell.tickButton.tag]) 
    [button setImage:[UIImage imageNamed:@"ticck.jpeg"] forState:UIControlStateNormal]; 
else 
    [cell.tickButton setImage:nil forState:UIControlStateNormal]; 
0

我猜你是給默認圖像到CustomCell類內部的按鈕,按鈕的標籤。這個問題在UITableviews中很常見。

您需要做的就是跟蹤已按下按鈕的單元格。做所有這些都在cellForRowAtIndexPath方法中。

例如:

更新的cellForRowAtIndexPath方法中的代碼

if (cell == nil) 
    { 
     //create new cell 
     cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 

如果(yourCondition == YES){

change the button image to the special image. 

} 否則{

keep the button image as default image. 

}

return cell;

要跟蹤單元格,您可以將單元格(其按鈕已被點擊)的索引路徑存儲在數組中。裏面的cellForRowAtIndexPath檢查當前indexPath存在於您的陣列中或不

1

當你設置的默認圖像按鈕也做

[button setImage:[UIImage imageNamed:@"ticck.jpeg"] forState:UIControlStateSelected]; 

在您的按鈕操作方法做到這一點:

-(IBAction)buttonWasPressed:(id)sender 
{ 
    UIButton *button = (UIButton *)sender; 
    if(button.selected==NO) { 
    [button setSelected:YES]; 

    } 

} 

希望這有助於。

3

您可以在CustomCell類中處理觸摸。只需在您的自定義類中移動方法-(IBAction)buttonWasPressed:(id)sender,並在init方法中添加 tickButton addTarget:selfaction:@selector(buttonWasPressed:)forControlEvents:UIControlEventTouchUpInside