更改另一部分中的按钮图像也会改变另一部分吗?

时间:2012-11-20 14:25:37

标签: iphone ios ipad

我有一个应用程序,我在两个不同的自定义类的表视图中加载两个类型的单元格。我的表视图中有5个部分,每个部分都有单行。我在单元格中添加了一个按钮,当点击时它会改变按钮的图像.as button.selected = YES; ike that.But我的问题是当我滚动单元格r重复时,即如果我选择一行中的按钮然后只有该行的背景图像需要改变。但在我的情况下,一些细胞按钮也在变化。这就是我如何添加东西。

- (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;
        cell.datelabel.text=@"skhfkhdf";
      cell.nameLabel.text=@"dsdbjsbjf";
    cell.msg.text=@"dshjshhshd";
 [cell.lbutton addTarget:self action:@selector(callAction:) forControlEvents:UIControlEventTouchUpInside];     
  cell.myImageView.image=[UIImage imageNamed:@"sdsd.png"];
  cell.llabel.text=@"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;
        cell.datelabel1.text=@"sdsdsdsd";
        cell.nameLabel1.text=@"sdsdsdsdsd";
        cell.msg1.text=@"sdsasfkdakfhk ";

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

        cell.llabel1.text=@"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;

`但这里的问题是它也改变了另一部分的按钮。任何人都可以帮助我吗?

2 个答案:

答案 0 :(得分:0)

我不确定我是否完全理解这个问题,但我认为你想在if后面创建后台创建。一旦它被改变你应该重置它们现在它不会被重置(就像你重置选择样式你应该重置背景)

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;
cell.datelabel.text=@"skhfkhdf";
cell.nameLabel.text=@"dsdbjsbjf";
cell.msg.text=@"dshjshhshd";
[cell.lbutton addTarget:self action:@selector(callAction:) forControlEvents:UIControlEventTouchUpInside];     
cell.myImageView.image=[UIImage imageNamed:@"sdsd.png"];
cell.llabel.text=@"sdjkjskljdls";   
cell.llabel.tag=indexPath.section+1;

return cell;

答案 1 :(得分:0)

您的if (indexPath.section == 0 && indexPath.row==0)检查条件可能存在问题。如果不是第一部分,此检查将始终为NO。因此,除了第一部分的第一行之外,您的单元格不会重复用于您的课程CustomCell