2012-11-20 90 views
0

我具有其中我加載兩種類型的細胞在表視圖從兩個不同的定製classes.I在我的表視圖5部分,每個部分具有單一rows.I正在添加一個按鈕以我的細胞並在其上點擊時的應用程序它將改變button.selected = YES按鈕。作爲的圖像; IKE that.But我的問題是當我滾動單元R的重複,即,如果我在一排然後選擇一個按鈕,行僅需要的背景圖像到change.But在我的情況下,一些小區的按鍵也changing.Here就是我如何加入things.`更改另一個部分中的按鈕圖像也更改?

- (UITableVieCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier1 = @"CellIdentifier"; 
    static NSString *CellIdentifier2 = @"Cell"; 
    if (indexPath.section == 0 && indexPath.row==0) 
    { 
     CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier1]; 

     if (cell == nil) 
     { 
      cell = [[[CustomCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier1] autorelease]; 
      cell.backgroundView = [ [[UIImageView alloc] initWithImage:[ [UIImage imageNamed:@"dhjkhs.png"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0] ]autorelease]; 


     } 
     cell.selectionStyle = UITableViewCellSelectionStyleNone; 
     [email protected]"skhfkhdf"; 
     [email protected]"dsdbjsbjf"; 
    [email protected]"dshjshhshd"; 
[cell.lbutton addTarget:self action:@selector(callAction:) forControlEvents:UIControlEventTouchUpInside];  
    cell.myImageView.image=[UIImage imageNamed:@"sdsd.png"]; 
    [email protected]"sdjkjskljdls"; 
     cell.llabel.tag=indexPath.section+1; 

     return cell; 

    } 
    else 
    { 


     Customcellwithimage *cell = (Customcellwithimage *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier2]; 

     if (cell == nil) 
     { 
      cell = [[[Customcellwithimage alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier2] autorelease]; 
      cell.backgroundView = [ [[UIImageView alloc] initWithImage:[ [UIImage imageNamed:@"sdsdsd.png"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0] ]autorelease]; 
     } 
     cell.selectionStyle = UITableViewCellSelectionStyleNone; 
     [email protected]"sdsdsdsd"; 
     [email protected]"sdsdsdsdsd"; 
     [email protected]"sdsasfkdakfhk "; 

     [cell.likebutton1 addTarget:self action:@selector(callAction:) forControlEvents:UIControlEventTouchUpInside]; 
     cell.myImageView1.image=[UIImage imageNamed:@"jpg1.png"]; 
     cell.cellview1.image=[UIImage imageNamed:@"adsdsd.png"]; 

     [email protected]"sdsdsdsdl"; 
     cell.llabel1.tag=indexPath.section+2; 
     cell.bannerview1.image=[UIImage imageNamed:@"dsdsd.png"]; 

     return cell; 







    } 



    return Nil; 

} 

i have 10 sections each returning 1 row.My problem is i need to change the image of the button when clicked.I was adding that button in the customcellwith image class as

[lbutton1 setImage:[UIImage imageNamed:@"sdsd.png"] forState:UIControlStateNormal]; 
[lbutton1 setImage:[UIImage imageNamed:@"sdsdsd.png"] forState:UIControlStateSelected]; 
lbutton1.selected=NO;` 

,並在我的代碼我已經添加`

UIButton *button = (UIButton *)sender; 
     button.selected=YES; 

`但這裏的問題是,它也改變also.Can任何其他部分的按鈕幫助我嗎?

回答

0

我不知道如果我完全理解這個問題,但我想你也想移動,如果後面的背景創作。一旦它將會改變你應該重新設置他們的方式,現在它不會先復位(就像你復位選擇風格,你應該重新設置背景)

if (cell == nil) 
{ 
    cell = [[[CustomCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier1] autorelease]; 
} 

cell.backgroundView = [ [[UIImageView alloc] initWithImage:[ [UIImage imageNamed:@"dhjkhs.png"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0] ]autorelease]; 

cell.selectionStyle = UITableViewCellSelectionStyleNone; 
[email protected]"skhfkhdf"; 
[email protected]"dsdbjsbjf"; 
[email protected]"dshjshhshd"; 
[cell.lbutton addTarget:self action:@selector(callAction:) forControlEvents:UIControlEventTouchUpInside];  
cell.myImageView.image=[UIImage imageNamed:@"sdsd.png"]; 
[email protected]"sdjkjskljdls"; 
cell.llabel.tag=indexPath.section+1; 

return cell; 
+0

實際上最好只創建一次圖像並緩存它,而不是每次創建它,但首先讓我們來檢查這是否是一個問題;順便說一句,謝謝編輯sunkehappy –

+0

不客氣。而'imageNamed:'將緩存系統的圖像。檢查這個https://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIImage_Class/Reference/Reference.html – sunkehappy

0

也許有一些問題,你的檢查條件if (indexPath.section == 0 && indexPath.row==0)。當它不是第一部分時,此檢查始終爲NO。所以,你的細胞不會被重用爲您CustomCell類,除了你的第一部分的第一行。

+0

但實際上我的問題是,當我點擊第二行上的按鈕,我正在改變它同時第4行中的按鈕也「第4行也改變,但在同一時間的按鈕」改變 –

+0

你說image.But?在同一時間?第四行按鈕改變時不滾動? – sunkehappy

+0

你是什麼意思,我嘗試了一切..? –