Iphone 所选UIButton在所选UITableViewCell内消失

Iphone 所选UIButton在所选UITableViewCell内消失,iphone,uitableview,uibutton,Iphone,Uitableview,Uibutton,我有一个表,我有CellForRowatineXpath委托,我有在该方法中创建的UITableViewCell实例,我返回它。在cellForRowAtIndexPath的某个地方,我还将UIButton添加到该单元格的cell.contentView。这一切都很好,直到我选择了有按钮的单元格。单元格将颜色更改为蓝色(这是正常的),uibutton也将其颜色更改为蓝色。现在,如果我点击所选单元格内的UIButton,它就会消失!哇,真奇怪,我该怎么修?我希望单击选定单元格中的UIButton时

我有一个表,我有CellForRowatineXpath委托,我有在该方法中创建的UITableViewCell实例,我返回它。在cellForRowAtIndexPath的某个地方,我还将UIButton添加到该单元格的cell.contentView。这一切都很好,直到我选择了有按钮的单元格。单元格将颜色更改为蓝色(这是正常的),uibutton也将其颜色更改为蓝色。现在,如果我点击所选单元格内的UIButton,它就会消失!哇,真奇怪,我该怎么修?我希望单击选定单元格中的UIButton时,该按钮保持不变

编辑:包括my cellForRowAtIndexPath实现

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
static NSString *sectionsTableIdentifier = @" sectionsTableIdentifier ";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: sectionsTableIdentifier];


    for (UIView *view in cell.contentView.subviews)
      [view removeFromSuperview];


     if (cell == nil) {
      cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero 
                reuseIdentifier: sectionsTableIdentifier] autorelease];

     }

     CGRect newIconRect = CGRectMake(276, 40, 29, 29);
      UIButton *newIcon = [[UIButton alloc] initWithFrame:newIconRect];
      [newIcon setImage:[UIImage imageNamed:@"mynewicon.png"] forState:UIControlStateNormal];
      [cell.contentView addSubview:newIcon];
      [newIcon release];


    return cell;

    }

按钮似乎以某种方式继承了选定状态作为其高亮显示状态。有人找到过解决办法吗

解决方案:我对这种黑客行为并不满意,但它符合我的目的。基本上,我希望按钮仅在选定单元格时显示。将UITableViewCell子类化并添加这些覆盖(editButton是在init中添加到单元格contentView的按钮实例)


我在OS 4.0测试版上注意到一个类似的问题,我在UITableViewCell中有一个UIButton,setImage:forState:UIControlStateNormal。选择一行并按下另一个表视图后,当我返回到表视图时,该按钮将消失。为了克服这一点,我还将image:forState:uicontrolstate设置为在图像上高亮显示。我不确定这是否能解决您的特定问题,但对我来说确实如此。我想在didSelectRowAtIndexPath:中设置button.highlighted=NO也可以,但我没有尝试。嘿,我知道这是一篇老文章,但这可能会有帮助: UITableView将尝试在其子视图的整个层次结构中传播其选择状态。如果一个子视图响应-setHighlighted:它将根据单元格选择状态进行更新

这解释了为什么按钮变为蓝色(高亮显示),但也解释了为什么按钮图像有时会消失:UIButton包含UIImageView实例以显示其图像,并将根据其状态([UIControl状态])交换每个图像视图的图像。但是“表格视图”单元格会将按钮图像视图的高亮显示标志设置为“是”(按钮没有预见到),但此图像视图的高亮显示状态没有图像,因此即使按钮根据其状态不断交换图像,您也看不到任何图像


我使用的解决方案是,无论何时选择或突出显示单元格,我都会将按钮图像视图的突出显示标志设置为NO.

我在这方面也遇到了问题(对于4.3及以下版本),并且上述解决方案对我不起作用。最后,设置按钮。调整SimageWhenHighlighted=否;在创建按钮时,我做到了这一点。

技巧是将UIButton作为子视图添加到UIImageView中

  • 创建UIImage

    UIImage backgroundImg = [UIImage imageName:@"yourimg"];
    UIImageView imageView = [UIImageView alloc]initWithImage:backgrounding];
    
  • 现在创建按钮

    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    // set the frame. the position of the button is the center 
    CGRect newIconRect = CGRectMake(imageView.center.x, imageView.center.y, 29, 29);
    
    button.frame = newIconRect;
    
  • 最后将按钮添加到UIImageView上

    [imageView addSubview:button];
    [cell addSubview imageView];
    

  • 现在,您不必使用高亮显示颜色。

    我们可以查看
    cellforrowatinexpath
    方法的内容吗
    tableView:DidSelectRowatineXpath:
    如果您已经实现了它,也会很有帮助。是的,我在编辑中包含了它,而我没有实现DidSelectRowatineXpath,只有CellForRowatineXpath当您说按钮“消失”时,它是隐藏了,还是实际上已经不存在了?如果你按下按钮应该在的区域,按钮的点击处理程序会被调用吗?它会变得“隐藏”,如果我再点击几次,按钮会再次出现,甚至颜色会从蓝色(选中)变回正常。我还有WillSelectRowatineXpath:(NSIndexPath*)indexPath方法,它只改变了一些与表无关的变量,并最终返回indexPath。我在iOS 4.3中仍然看到这个问题。这种变通办法似乎仍然有效。
    [imageView addSubview:button];
    [cell addSubview imageView];