滚动时不加载tableViewCell

时间:2017-04-22 01:07:31

标签: ios swift uitableview firebase tableviewcell

我知道这个问题正在打败一匹死马,但我已经搜索了所有以前的问题,并且没有找到可以提供帮助的解决方案。我正在使用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()
    }

我通过“loadImageUsingCachWithUrlString”函数访问此扩展程序

进入我的表视图,我在每个单元格中加载用户名age age等,我有Firebase持久性(如果有所不同),我将用户信息下载到单元格的方式是通过这个

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! UserCell
    let cells = tableView.visibleCells
    let user = users[indexPath.row]
    let profileImageUrlThird = user.profileImageUrl
    let tableViewHeight = tableView.bounds.size.height
    cell.nameLabel.text = user.name
    cell.bioLabel.text = user.aboutMe
    cell.reviewRating.text = user.averageRating
    cell.profileImageView.loadImageUsingCachWithUrlString(urlString: profileImageUrlThird!)
    let tempNumber = user.profileView
    let stringTemp = tempNumber?.stringValue
    cell.profileViewCount.text = stringTemp
    cell.imageView?.contentMode = .scaleAspectFill
    cell.backgroundColor = UIColor.clear
    cell.textLabel?.font = UIFont(name:"Avenir", size:22)
    tableView.layer.borderWidth = 0;
    tableView.layer.borderColor = UIColor.lightGray.cgColor
    print(tableView.frame.height,tableView.frame.width)
    //tableview height is 670 table view width is 414
    print(cell.frame.width,cell.frame.height)
    //cell width is 414 cell height is 330


    for cell in cells {
        cell.transform = CGAffineTransform(translationX: 0, y: tableViewHeight)
    }

返回细胞 }

信息加载,一切都很好,但一旦我拖动细胞消失。任何帮助将不胜感激,我已经坚持了近一个星期

1 个答案:

答案 0 :(得分:0)

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!
}