【问题标题】:UIImageView is displaying not correctUIImageView 显示不正确
【发布时间】:2018-06-11 16:47:52
【问题描述】:

我已将 UIImageView 放置在我的自定义单元格中(Swift 3):

let cell = self.tableView.dequeueReusableCell(withIdentifier: "Feedcell", for: indexPath) as! FruitTableViewCell

let hero: List_Feed
        hero = heroes[indexPath.row]

if(hero.photo==""){
            cell.cnHeight.constant = 0
            cell.cnTopMargin.constant = 0
            cell.cnBottomMargin.constant = 52
            cell.imgPhoto.image = UIImage()
            cell.imgPhoto.frame = CGRect(x: 0, y: 0, width: 0, height: 0)
        }else{
            cell.cnTopMargin.constant = 14
            cell.cnBottomMargin.constant = 50
            cell.imgPhoto.frame = CGRect(x: 0, y: 0, width: 0, height: 0)
            //displaying image
            Alamofire.request(hero.photo!).responseImage { response in
                debugPrint(response)
                if let image = response.result.value {
                    //let newWidth = containerView.frame.height * ratio
                    let bounds = UIScreen.main.bounds
                    let width = bounds.size.width
                    let imagewidth = image.size.width
                    let imageheight = image.size.height
                    let ot1 = width / imagewidth
                    let ot2 = round(ot1 * imageheight)
                    //let ot3 = round(ot2 / 2)
                    //let ot4 = ot3 - 12
                    //let ot5 = ot4 + 50
                    cell.cnHeight.constant = ot2
                    cell.imgPhoto.image = image
                }
            }
        }

它应该是这样显示的:

这是我向下滚动然后向上滚动时的显示方式:

(突然 uiimageview 覆盖了所有自定义单元格空间)

【问题讨论】:

  • 你用的是autolaout,那为什么cell.imgPhoto.frame
  • 在不断变化之后,你在做 layoutIfNeeded() 吗?
  • 为什么当单元格显示时你一次又一次地下载图像? .你应该使用SDWebImage库或者alamofire图片库来缓存图片。

标签: ios swift uitableview uiimageview


【解决方案1】:

尝试添加

DispatchQueue.main.async {
    // all your ui code
}

您可能仍在后台线程中。

【讨论】:

  • 我应该在哪里插入这个方法?课后:ClassName?还是在 viewDidLoad 之后?
  • 在响应中。所有这些代码都需要在响应中的 this 中。
  • 还是随机显示图片和单元格,有时是对的,但有时图片覆盖了整个单元格
  • 而且图像在滚动时变成白色
  • 在手机上,所以我是有限的。我会说在每次调用之前将帧设置为 0 可能不是最好的。你如何给表格视图单元格的高度
猜你喜欢
  • 1970-01-01
  • 2018-07-18
  • 2013-10-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多