如何维护UITableView中特定按钮的状态(即停止覆盖图像)

时间:2012-10-08 07:00:53

标签: iphone objective-c ios uitableview uibutton

我有一个iPhone应用程序问题一直困扰着我几天,看起来真的不应该这么难,所以我肯定我错过了一些明显的东西。我已经研究过很多关于“类似”主题的论坛讨论,但没有任何实际解决这个问题的内容,特别是。

要明确的是,如果有一些文件或其他一些我应该研究的来源,请指出我正确的方向。

这里......

我有一个项目列表,我在表格中向用户显示(uitableview)。每个项目的单元格(uitableviewcell)是自定义的,包含两个图像按钮(uibuttons:绿色和红色)。正如所料,对于表中的每个项目,用户可以单击任何按钮。基于一个名为monitoringRequestType的按钮参数,该按钮调用一个单独的进程来更新服务器。如果状态为“已批准”,则图像分别变为“已批准”和“拒绝”。因此,当我点击红色按钮时,服务器会将状态更新为“已拒绝”,然后图像会更改为“已批准”和“已拒绝”。 简单,对吧?

所以,问题在于:

点击拒绝按钮后,“批准”图像会显示在“已经批准”的图像之上(因此可以看到两个图像),而“已经注入”的图像可以正常工作。为简洁起见,我只在此处包含相关代码(希望格式正确):

CellForRow:
if(indexPath.section==0){
    NSDictionary *dict=[saveJson objectAtIndex:indexPath.row];
    NSString* sMonitoringType = [dict valueForKey:@"monitoringType"];
    UIButton *button1= [[UIButton alloc] initWithFrame:CGRectMake(230,10,40,40)];
    UIButton *button2= [[UIButton alloc] initWithFrame:CGRectMake(280,10,40,40)];
    if([sMonitoringType compare:@"Pending"] == NSOrderedSame){
        [button1 setImage:[UIImage imageNamed:@"approve"]     
                 forState:UIControlStateNormal];
        [button1 addTarget:self
                    action:@selector(greenButtonPressed:withEvent:) 
          forControlEvents:UIControlEventTouchUpInside];
         button1.tag= indexPath.row;   
        [button2 setImage:[UIImage imageNamed:@"reject"]
                 forState:UIControlStateNormal];
        [button2 addTarget:self
                    action:@selector(redButtonPressed:withEvent:) 
          forControlEvents:UIControlEventTouchUpInside];
         button2.tag= indexPath.row;             
    } else if([sMonitoringType compare:@"Approved"] == NSOrderedSame){
        [button1 setImage:[UIImage imageNamed:@"alreadyapproved"] 
                 forState:UIControlStateNormal];
        [button2 setImage:[UIImage imageNamed:@"reject"]
                 forState:UIControlStateNormal];
        [button2 addTarget:self
                    action:@selector(redButtonPressed:withEvent:) 
          forControlEvents:UIControlEventTouchUpInside];
         button2.tag= indexPath.row;
    } else if([sMonitoringType compare:@"Rejected"] == NSOrderedSame){
        [button1 setImage:[UIImage imageNamed:@"approve"] 
                 forState:UIControlStateNormal];
        [button2 setImage:[UIImage imageNamed:@"alreadyrejected"] 
                 forState:UIControlStateNormal];
        [button1 addTarget:self
                    action:@selector(greenButtonPressed:withEvent:) 
          forControlEvents:UIControlEventTouchUpInside];
         button1.tag= indexPath.row;
    }
    [cell addSubview:button1];
    [cell addSubview:button2];
    [button1 release];
    [button2 release];
}
return cell;
}

1 个答案:

答案 0 :(得分:0)

每当细胞重新加载时,UIButton被添加到细胞的子视图中(你正在重复使用细胞,对吧?)。您应该重用已经添加到单元格子视图中的旧按钮,而不是添加新按钮,只是为了验证问题,停止重用单元格(内存效率低,不应该在生产代码中完成!)。要重复使用旧按钮,请为每个按钮设置一个唯一标记,然后将其作为[cell viewWithTag:uniqueTag]

返回
if(indexPath.section==0){
  NSDictionary *dict=[saveJson objectAtIndex:indexPath.row];
  NSString* sMonitoringType = [dict valueForKey:@"monitoringType"];
  UIButton *button1= [cell viewWithTag:900];
  UIButton *button2= [cell viewWithTag:901];
  if(!button1){// button1 doesn't exist yet (first time)
    button1 = [[UIButton alloc] initWithFrame:CGRectMake(230,10,40,40)];
    [cell addSubview:button1];
  }
  if(!button2){// button2 doesn't exist yet (first time)
    button1 = [[UIButton alloc] initWithFrame:CGRectMake(280,10,40,40)];
    [cell addSubview:button2];
  }
  ......