繁体   English   中英

当我滚动 UITableView 时图像消失

[英]Image Disappears when i am scrolling UITableView

我正在使用UITableView来显示我的数据。

我的数据由图像和文本组成。 问题是当我滚动UITableView ,图像消失了。

这是我的代码:

func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return msgs.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! collCell
    let msg = msgs[indexPath.row]
    var decodeImgURL = ""
    cell.lbl1.text = msg.content

    decodeImgURL = msg.imgurl

    if decodeImgURL == "none" {
        cell.img.isHidden = true
    } else {

        let dataDecoded : Data = Data(base64Encoded:decodeImgURL,options: .ignoreUnknownCharacters)!
        let decodedImage = UIImage(data: dataDecoded)
        cell.img.image = decodedImage

    }

    return cell
}

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {

    let msg = msgs[indexPath.row]

    if msg.imgurl == "none" {
        return 64
    } else {
        return 207
    }

}

使用UITableView ,内存中唯一的单元格是可见单元格。 当您将表格单元格从屏幕上滚动时,它会从内存中删除。 这有助于表格视图滚动得更快。 您面临的问题是,当您将表格单元格从屏幕上滚动时,单元格(连同图像)会从内存中释放。 当您将单元格滚动回屏幕时,它会再次分配到内存中。 我认为您的问题可能来自以下两个问题之一:

1) 您只需要等待几秒钟即可在单元格中重新创建您的图像。

2)也许您的msgs数组中存在问题。 如果几秒钟后图像没有出现,请尝试在此行之后放置一个断点: cell.img.image = decodedImage 如果在将您的单元格从屏幕上滚动然后返回到屏幕上后未到达该断点,则问题是decodeImgURL (您的图像的 url)不正确。

奇怪,因为Swift5仍然没有解决方案

最简单的选择是每个时间段更新图像(假设为 1 秒)

    Timer.scheduledTimer(withTimeInterval: 1, repeats: true) { timer in

        self.pictureImageView?.image = your_image
    }

然后在viewDidDisappear()方法中使用timer.invalidate()取消分配 Timer。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM