【问题标题】:tableViewCell not loading when scrolling滚动时未加载uitableViewCell
【发布时间】:2017-04-22 01:07:31
【问题描述】:

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

进入我的表格视图,我在每个单元格中加载用户姓名年龄生物等,我有 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)
    }

返回单元格 }

信息加载,一切都很好,但只要我拖动单元格就会消失。任何帮助将不胜感激,我已经坚持了将近一周

【问题讨论】:

    标签: ios swift uitableview firebase tableviewcell


    【解决方案1】:

    tableView.dequeueReusableCell() 可能会返回 nil 值。结果reuse pool中没有这样的cell,需要自己生成一个。

    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值,否则它会崩溃。只需将其更改为? ,就是语言设计
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多