Ios 滚动时未加载tableViewCell

Ios 滚动时未加载tableViewCell,ios,swift,uitableview,firebase,tableviewcell,Ios,Swift,Uitableview,Firebase,Tableviewcell,我知道这个问题是白费力气,但我已经搜索了前面所有的问题,还没有找到一个有帮助的解决方案。我正在使用Firebase作为我的后端和存储。当用户上传照片时,它会进入我的Firebase存储,并遵循缓存照片的教程来轻松处理数据。我访问缓存数据的方式是: let imageCache = NSCache<NSString, UIImage>() extension UIImageView { func loadImageUsingCachWithUrlString(urlString:

我知道这个问题是白费力气,但我已经搜索了前面所有的问题,还没有找到一个有帮助的解决方案。我正在使用Firebase作为我的后端和存储。当用户上传照片时,它会进入我的Firebase存储,并遵循缓存照片的教程来轻松处理数据。我访问缓存数据的方式是:

let imageCache = NSCache<NSString, UIImage>()

extension UIImageView {

func loadImageUsingCachWithUrlString(urlString: String) {

    self.image = nil
    if let cachedImage = imageCache.object(forKey: urlString as NSString) as UIImage?{
        self.image = cachedImage

        return
    }


    let url = URL(string: urlString)
        URLSession.shared.dataTask(with: url!, completionHandler: { (data,response,error) in
            if error != nil{
                print(error as Any)
                return
            }
            DispatchQueue.main.async {
                if let downloadedImage = UIImage(data: data!){
                    imageCache.setObject(downloadedImage, forKey: urlString as NSString)
                    self.image = downloadedImage
                }
            }
        }).resume()
    }
返回单元 }


信息加载,一切正常,但只要我拖动单元格,单元格就会消失。如果您有任何帮助,我将不胜感激。我已经在这个问题上纠缠了将近一周了。

tableView.dequeueReusableCell()
可能会返回一个
nil
值。所以,重用池中并没有这样的单元,所以您需要自己生成一个单元

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let identifer = "cell"
    var cell = tableView.dequeueReusableCell(withIdentifier: identifer)
    if cell == nil {           // when no reusable cell 
      cell = UITableViewCell(style: .default, reuseIdentifier: identifer) 
    }
    cell?.textLabel?.text = "row:\(indexPath.row)"
    return cell!
}

tableView.dequeueReusableCell()
可能返回一个
nil
值。所以,重用池中并没有这样的单元,所以您需要自己生成一个单元

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let identifer = "cell"
    var cell = tableView.dequeueReusableCell(withIdentifier: identifer)
    if cell == nil {           // when no reusable cell 
      cell = UITableViewCell(style: .default, reuseIdentifier: identifer) 
    }
    cell?.textLabel?.text = "row:\(indexPath.row)"
    return cell!
}

谢谢你的回复。因此,基本上检查单元格是否等于nil?对,您可以参考上面的代码并对其进行测试。我收到一条警告,上面说“比较类型的非可选值”tableView。UserCell“到nil总是返回false”这是引起关注的原因,还是忽略它?这是因为您使用“as!”将可选值扭曲为非nil值,这意味着该值必须是非nil值,否则将崩溃。只需将其更改为,这是语言设计,谢谢你的回复。因此,基本上检查单元格是否等于nil?对,您可以参考上面的代码并对其进行测试。我收到一条警告,上面说“比较类型的非可选值”tableView。UserCell“到nil总是返回false”这是引起关注的原因,还是忽略它?这是因为您使用“as!”将可选值扭曲为非nil值,这意味着该值必须是非nil值,否则将崩溃。只需将其更改为,这是语言设计