2012-11-20 43 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

你是什么意思,我尝试了一切..? –