简体   繁体   English

当我滚动 UITableView 时图像消失

[英]Image Disappears when i am scrolling UITableView

I'm using UITableView to display my data.我正在使用UITableView来显示我的数据。

My data consist of an image and text.我的数据由图像和文本组成。 The problem is when I'm scrolling the UITableView the image disappears.问题是当我滚动UITableView ,图像消失了。

This is my code:这是我的代码:

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
    }

}

With a UITableView , the only cells that are in memory are the visible cells.使用UITableView ,内存中唯一的单元格是可见单元格。 When you scroll a table cell off the screen, it is removed from memory.当您将表格单元格从屏幕上滚动时,它会从内存中删除。 This helps the table view scroll faster.这有助于表格视图滚动得更快。 The issue you are facing is that when you scroll the table cell off the screen, the cell (along with the image) are deallocated from memory.您面临的问题是,当您将表格单元格从屏幕上滚动时,单元格(连同图像)会从内存中释放。 When you scroll the cell back onto the screen it is allocated into memory again.当您将单元格滚动回屏幕时,它会再次分配到内存中。 I think your issue could be from one of two problems:我认为您的问题可能来自以下两个问题之一:

1) You just need to wait a few seconds for your image to be re-created in the cell. 1) 您只需要等待几秒钟即可在单元格中重新创建您的图像。

2) Perhaps there is an issue in your msgs array. 2)也许您的msgs数组中存在问题。 If the image does not appear after a couple seconds, try to put a breakpoint right after this line: cell.img.image = decodedImage .如果几秒钟后图像没有出现,请尝试在此行之后放置一个断点: cell.img.image = decodedImage If that breakpoint is not reached after scrolling your cell off the screen then back onto the screen, then the issue is that the decodeImgURL (url of your image) is incorrect.如果在将您的单元格从屏幕上滚动然后返回到屏幕上后未到达该断点,则问题是decodeImgURL (您的图像的 url)不正确。

Strange, because there are no solutions still for Swift5奇怪,因为Swift5仍然没有解决方案

The simplest option is to update the image each period of time (let's say 1 second)最简单的选择是每个时间段更新图像(假设为 1 秒)

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

        self.pictureImageView?.image = your_image
    }

Then deallocate the Timer with timer.invalidate() at viewDidDisappear() method.然后在viewDidDisappear()方法中使用timer.invalidate()取消分配 Timer。

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

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