从Firebase Swift加载后,单元格未显示图像 [英] Cell not showing image after loaded from Firebase Swift

查看:47
本文介绍了从Firebase Swift加载后,单元格未显示图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个UITableViewCell,它显示图像或文本.我已将UIImageView的高度和宽度设置为< = 150,以便在图像不存在但文本出现的情况下,它不会影响单元格的高度.(这是一个消息传递应用程序)

I have a UITableViewCell which shows either an image or text. I have set my UIImageView height and width to <= 150 so that it wouldn't affect the cell height in case the image isn't present but the text is. (this is a messaging app)

现在我的问题是,单元格在加载后不显示图像,用户需要返回,然后再次查看此屏幕以查看图像.我该如何解决呢?

Now my problem is, the cell does not show the image after it's loaded and user needs to go back, and view this screen again in order to see the image. How should i solve this?

在加载图像之前:

成功加载图像后:

如您所见,它没有显示,只有视图被扩展了

as u can see, it doesnt show, only the view was expanded

如果用户按下BACK并再次出现在此视图中,

现在显示图像.

我应该如何解决这个问题?因为我无法在tableviewcell中编写 tableview.reloaddata().

how should i solve this issue? because I am not able to write tableview.reloaddata() in tableviewcell.

在tableviewcell中用于配置我的单元格的代码:

for my code in my tableviewcell to configure my cell:

func configCell(message: Message) {

    self.message = message

    if message.fromId == currentUser {

        sentView.isHidden = false

        sentMsgLabel.text = message.textMessages

        receivedMsgLabel.text = ""

        receivedView.isHidden = true

        timeReceived.text = ""

        timeSent.text = message.timestamp

        youLabel.text = "You"

        themLabel.text = ""


        if let ImageUrl = message.imageUrlLink {

            self.receivedimg.loadImageUsingCacheWithUrlString(ImageUrl)
        }
        }

        else {

        sentView.isHidden = true

        sentMsgLabel.text = ""

        receivedMsgLabel.text = message.textMessages

        receivedMsgLabel.isHidden = false

        timeReceived.text = message.timestamp

        timeSent.text = ""

        // setting name for receipient
        Database.database().reference().child("users").child(message.fromId!).child("name").observe(.value) { (datasnapshot) in
        if let name = datasnapshot.value as? String {
                self.themLabel.text = name
            }
        }

        youLabel.text = ""
    }
}

对于我的loadImageUsingCacheWithUrlString代码将是如下扩展:

for my loadImageUsingCacheWithUrlString code would be an extension as below:

let imageCache = NSCache<AnyObject, AnyObject>()

    extension UIImageView {

    func loadImageUsingCacheWithUrlString(_ urlString: String) {

        self.image = nil

        //check cache for image first
        if let cachedImage = imageCache.object(forKey: urlString as AnyObject) as? UIImage {
            self.image = cachedImage
            return
        }

        //otherwise fire off a new download
        let url = URL(string: urlString)
        URLSession.shared.dataTask(with: url!, completionHandler: { (data, response, error) in

            //download hit an error so lets return out
            if error != nil {
                print(error ?? "")
                return
            }

            DispatchQueue.main.async(execute: {

                if let downloadedImage = UIImage(data: data!) {
                    imageCache.setObject(downloadedImage, forKey: urlString as AnyObject)

                    self.image = downloadedImage
                }
            })

        }).resume()
    }

}

推荐答案

最后我尝试了很多方法.在我的ViewDidload中,我只是添加了一个延迟

I have tried so many ways, in the end..In my ViewDidload, I just added a delay

 DispatchQueue.main.asyncAfter(deadline: .now() + 1){
        self.tableView.reloadData()
    }

需要延迟.没有它,它是行不通的.我认为这是由于在完成图像加载之前已设置并加载了单元格的高度,所以表格单元格不知道需要多少高度,这就是为什么我们需要在延迟1秒后重新加载表格视图

Delay is required. Without it, it doesn't works. I am assuming it's due to the cell heights were set up and loaded before the image is finished loading, so the tablecell didn't know how much height was needed, that is why we need to reload the tableview after 1 second delay

这篇关于从Firebase Swift加载后,单元格未显示图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆