UITableView在后台加载ONCE所有单元格

时间:2016-10-24 11:56:35

标签: uitableview swift3

美好的一天!很简单的问题。 我使用dequeueReusableCell在后台加载自定义单元格,因为我从url加载图像。

问题是当我开始滚动时。 TableView同步开始重新加载单元格,因此它们会闪烁"内容不同。

如果我慢慢滚动,它会闪烁并最终用正确的内容填充单元格,但如果我更快地填充它,它可以随机播放内容。很有趣,但它仍然是一个问题,

我认为最好的解决方案是加载一次单元格,然后滚动静态表格。有可能??

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    if self.dialogs[indexPath.item].fromID == profileID {
        let cell = tableView.dequeueReusableCell(withIdentifier: "dialogMeCell", for: indexPath) as! CellForDialogMe
        DispatchQueue.global().async {
            let photoURL = self.partners[indexPath.item].userPhoto as! String
            let imageData = try? Data(contentsOf: URL(string: photoURL.addingPercentEncoding(withAllowedCharacters: NSCharacterSet.urlQueryAllowed)!)!)
            let partnerPhoto = UIImage(data: imageData!)
            DispatchQueue.main.async {
                let partnerName = "\(self.partners[indexPath.item].userName!) " + "\(self.partners[indexPath.item].userSurname!)"
                var messageText = ""
                if self.dialogs[indexPath.item].hasAttachments! == true {
                    messageText = "Attachment"
                }
                else {
                    messageText = self.dialogs[indexPath.item].text!
                }
                if self.dialogs[indexPath.item].readState == false {
                    cell.message.textColor = UIColor.white
                    cell.message.backgroundColor = UIColor(red: 0.561, green: 0.651, blue: 0.757, alpha: 1.00)
                }
                cell.fillWithContent(partnerPhoto: partnerPhoto!, partnerName: partnerName, message: messageText, selfPhoto: self.profilePhoto!)
            }
        }
        return cell
    }
else {
        let cell = tableView.dequeueReusableCell(withIdentifier: "dialogHimCell", for: indexPath) as! CellForDialogHim
        DispatchQueue.global().async {
            let photoURL = self.partners[indexPath.item].userPhoto as! String
            let imageData = try? Data(contentsOf: URL(string: photoURL.addingPercentEncoding(withAllowedCharacters: NSCharacterSet.urlQueryAllowed)!)!)
            let partnerPhoto = UIImage(data: imageData!)
            DispatchQueue.main.async {
                let partnerName = "\(self.partners[indexPath.item].userName!) " + "\(self.partners[indexPath.item].userSurname!)"
                var messageText = ""
                if self.dialogs[indexPath.item].hasAttachments! == true {
                    messageText = "Attachment"
                }
                else {
                    messageText = self.dialogs[indexPath.item].text!
                }
                if self.dialogs[indexPath.item].readState == false {
                    cell.partnerName.textColor = UIColor.white
                    cell.message.textColor = UIColor.white
                    cell.backgroundColor = UIColor(red: 0.561, green: 0.651, blue: 0.757, alpha: 1.00)
                }
                cell.fillWithContent(partnerPhoto: partnerPhoto!, partnerName: partnerName, message: messageText)
            }
        }
        return cell
    }
}

谢谢!

1 个答案:

答案 0 :(得分:0)

最佳解决方案是缓存图像。如果图像不在缓存中,则只进行异步请求。如果您在缓存中有图像,则只显示缓存的图像。虽然您可以只持有[String:UIImage]的字典并使用URL作为键,但这并不是非常友好或带宽友好的,因为它不会持久存在。你可以写一个更复杂的缓存,但其他人已经这样做了所以我建议你只使用他们中的一个:SDwebImage AlamofireImage