【问题标题】:Changing the button image in one section changing the other also?更改一个部分中的按钮图像也更改另一个部分?
【发布时间】:2012-11-21 18:45:22
【问题描述】:

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

- (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;

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

【问题讨论】:

    标签: iphone ios ipad


    【解决方案1】:

    您对if (indexPath.section == 0 && indexPath.row==0) 的检查条件可能有问题。当它不是第一部分时,此检查将始终NO。因此,您的单元格将不会被您的班级 CustomCell 重复使用,除了您的第一部分的第一行。

    【讨论】:

    • 但实际上我的问题是当我点击第二行的按钮时,我正在改变它的图像。但同时第四行的按钮也在改变
    • 你说'但同时第 4 行的按钮也在改变'?在同一时间?第 4 行按钮更改时没有滚动?
    • 我的意思是如果您在点击按钮后滚动。可能原因是你的按钮被重用了(状态也被重用了)。
    • 你能帮我如何有效地使用它
    • 我不确定。您可以在表视图的数据源委托方法中设置cell.lbutton.selected = NO。即在重用该按钮时将其设置为正常。试一试。
    【解决方案2】:

    我不确定我是否完全理解这个问题,但我认为你想将后台创建也移到 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;
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-04
    • 1970-01-01
    • 1970-01-01
    • 2015-05-06
    • 1970-01-01
    • 2023-01-17
    相关资源
    最近更新 更多