【问题标题】:How to reload only one cell in a collectionView?如何在collectionView中只重新加载一个单元格?
【发布时间】:2018-10-31 17:03:15
【问题描述】:

我有一个collectionView,我有两个问题。首先,滚动太慢了。我的 collectionView 从 url 中的 API 接收数据,如下所示:

这是我的代码:

extension ViewController: UICollectionViewDelegate, UICollectionViewDataSource {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        if imageUrl.count >= 4 {
            activity.stopAnimating()
            activity.isHidden = true
        }else{
            DispatchQueue.main.async {
                self.myCollectionView.reloadData()
            }
        }

        return imageUrl.count
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = myCollectionView.dequeueReusableCell(withReuseIdentifier: "myViewCell", for: indexPath) as! myViewCell

        if let url = NSURL(string: imageUrl[indexPath.row]){
            if let data = NSData(contentsOf: url as URL){
                cell.carImageView.contentMode = UIViewContentMode.scaleAspectFit
                cell.carImageView.image = UIImage(data: data as Data)
            }
        }

        cell.firstLabel.text = firstLabelArray[indexPath.row]

        if secondLabelArray != []{
            cell.secondLabel.text = secondLabelArray[indexPath.row]
        }

        return cell
    }
}

我遇到的第二个问题是我只需要刷新集合视图中的第三个单元格。那是我的第二个标签,但是当我尝试使用 myCollectionView.reloadData() 时,它会重新加载我的所有单元格。我需要每秒刷新第三个单元格。

【问题讨论】:

  • let data = NSData(contentsOf: url as URL)。不。这阻塞了当前线程,它恰好是主线程,所以被阻塞的是 UI。使用异步网络呼叫。 SDWebImage, KingFisher, Alamofire+Image, AFNetworking + Image 做得很好。
  • this 是您要找的吗?
  • 使用 self.collectionView.reloadItems(at: [IndexPath]) 重新加载特定单元格
  • 我还认为您对collectionView(_:numberOfItemsInSection) 的实施会给您带来问题。如果您在调用reloadData() 的结果中调用reloadData(),我认为您将无限递归。

标签: ios swift uicollectionview


【解决方案1】:

要加载特定单元格,请使用以下命令:

let indexPath = IndexPath(item: 2, section: 0)
collectionView.reloadItems(at: [indexPath])

你正在加载cellForItemAt中的图片, 只要确保您没有在主线程上加载图像

您可以使用库来下载图像并缓存图像

https://github.com/SDWebImage/SDWebImage

https://github.com/Alamofire/AlamofireImage

【讨论】:

    【解决方案2】:

    collectionView 的延迟问题可能是你使用 NSData 的方式,考虑使用 Alamofire Image,像这样

    if imageUrl.count > 0 {
            let eachImage = imageUrl[indexPath.row]
            Alamofire.request(eachImage).responseImage(completionHandler: {(response) in
                print("answer\(response)")
                if let image = response.result.value{
                    DispatchQueue.main.async {
                        cell.ImageView.image = image
                    }
                }
            })
            }
    

    【讨论】:

      【解决方案3】:

      Collection View laggy 因为您试图在主线程上显示来自 url 的图像。在从 api 下载图像之前在单元格中显示一些默认图像,然后在主线程上更新单元格

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-12-10
        • 1970-01-01
        • 2013-06-23
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多