【问题标题】:UITableViewCell : UIImageView in ContentView overlaps images in iPhone SDK.UITableViewCell : ContentView 中的 UIImageView 与 iPhone SDK 中的图像重叠。
【发布时间】:2011-01-19 05:52:35
【问题描述】:

在我的 iPhone 应用程序中,我正在使用 tableView 并将 imageView 添加为我的 tableViewcell 中的 ContentView。

单击按钮时,我正在更改 tableView 内容。所以每次我点击一个按钮时,它应该在 tableViewCell 中显示相应的图像。

问题:

假设我分别在点击按钮 A、B 和 C 时在 tableView 中加载图像 1.png、2.png 和 3.png。

现在让我们考虑一下,最初我单击按钮 A 并出现 1.png。现在,当我单击按钮 B 时,会出现 2.png,但 1.png 也保留在背景中。所以基本上它与 tableViewCell 重叠了一个新图像。

我已经解决了问题:

1) 我尝试释放 imageView 并设置 imageView.image = nil;

2) 我尝试在每次单击按钮之前重新加载表格并清空表格。

3) 即使我尝试将整个代码放在cellForRowAtIndexPath 方法的 if(cell==nil) 子句中

【问题讨论】:

标签: iphone cocoa-touch uitableview uiimageview


【解决方案1】:

您是否尝试过清除单元格的 contentView?

for(UIView* subview in [cell.contentView subviews]) {
   [subview removeFromSuperview];
}

【讨论】:

  • 哦,你非常接近回答我的需要。
【解决方案2】:

如果您使用三个不同的图像视图,请尝试 imgView1.hidden=FALSE;imgView.hidden=TRUE;imgView3.hidden=TRUE on button1 click 事件。 button2 和 button3 的方法相同。

【讨论】:

    【解决方案3】:

    您似乎已将图像设置在 (cell==nil) 块中。如果是,请将其注释掉。

    这样做:

    if(cell ==nil){
    //add image view here.
    }
    
    UIImageView *imgView = (UIImageView *)[cell.contentView viewWithTag:ImgViewTag];
    if(condition)
       imgView.image = someImage;
    else
       imgView.image = nil;
    

    【讨论】:

    • 感谢您的帮助。我得到了答案
    • @PARTH 很高兴你得到了答案。但是如果每次重新加载表格时都需要更改 imageview 的图像,那么每次需要时删除和添加 imageview 并不是一个好方法。您应该只更改它的图像,否则您的表格将无法平滑滚动。这只是我的建议,可能是您正在做一些完全不同的事情并且删除图像视图适合您的情况。
    • 我不是静态分配图像。我从数据库中获取它,我正在重新加载 tableview,但是即使在重新加载 tableview 之后,图像也会重叠。不过感谢您的建议:)
    【解决方案4】:

    我已经得到了答案,我真的很感谢 Ben Gottlieb:

    remove image from tableViewCell created programmatically

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-24
      • 2012-10-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多