【问题标题】:Image in table view [duplicate]表格视图中的图像 [重复]
【发布时间】:2017-06-18 18:40:12
【问题描述】:

我对表格视图单元格中的图像有疑问 图片是从 Google Firebase 下载的,在每个单元格中都有一个 但是当我向上或向下滚动时,图像会自动更改索引 这是我的代码,有人可以帮助我吗?非常感谢!

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    if postsArray[indexPath.row].imageNoImage == true{

        let cell = tableView.dequeueReusableCell(withIdentifier: "imageLook", for: indexPath) as! imageLookTableViewCell
        cell.authorLabel.text = self.postsArray[indexPath.row].author
        cell.likesCountLabel.text = "\(self.postsArray[indexPath.row].likes!)"
        cell.postID = postsArray[indexPath.row].postId
        cell.textViewPost.text = self.postsArray[indexPath.row].textPost
        let url = URL(string: postsArray[indexPath.row].pathToImage as! String)
        if url != nil {
            DispatchQueue.global().async {
                let data = try? Data(contentsOf: url!)
                DispatchQueue.main.async {
                    if data != nil {
                        cell.imagePost.image = UIImage(data:data!)
                    }else{

                    }
                }
            }
        }
        for person in self.postsArray[indexPath.row].peopleWhoLike {
            if person == FIRAuth.auth()!.currentUser!.uid {
                cell.likesBtn.isHidden = false
                break
            }
        }

        return cell
    }

【问题讨论】:

  • 不要问已经有数百万答案的问题。

标签: ios swift uitableview tableview


【解决方案1】:

您的问题不是很具有描述性/写得很好,但我认为您的问题是您没有缓存图像。

试试这个:

    let imageCache = NSCache()
    let cell = tableView.dequeueReusableCell(withIdentifier: "imageLook", for: indexPath) as! imageLookTableViewCell
    cell.authorLabel.text = self.postsArray[indexPath.row].author
    cell.likesCountLabel.text = "\(self.postsArray[indexPath.row].likes!)"
    cell.postID = postsArray[indexPath.row].postId
    cell.textViewPost.text = self.postsArray[indexPath.row].textPost
    let url = URL(string: postsArray[indexPath.row].pathToImage as! String)

    if url != nil {
        if let cachedImage = imageCache.objectForKey(url) as? UIImage {
            cell.imagePost.image = cachedImage
            return
        }
        DispatchQueue.global().async {
            let data = try? Data(contentsOf: url!)
            DispatchQueue.main.async {
                if data != nil {
                    if let myImageName = UIImage(data:data!){
                        imageCache.setObject(myImageName, forKey: url)
                    }
                    cell.imagePost.image = UIImage(data:data!)
                }else{

                }
            }
        }
    }
    for person in self.postsArray[indexPath.row].peopleWhoLike {
        if person == FIRAuth.auth()!.currentUser!.uid {
            cell.likesBtn.isHidden = false
            break
        }
    }

    return cell
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-13
    • 1970-01-01
    • 2012-08-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多