Ios UICollectionView将在调用;“重新加载项目”;方法

Ios UICollectionView将在调用;“重新加载项目”;方法,ios,uicollectionview,uicollectionviewcell,uicollectionviewlayout,Ios,Uicollectionview,Uicollectionviewcell,Uicollectionviewlayout,我的手机有一个可以从网络下载的图像,因此我需要将手机的高度设置为动态。 图像下载完成后,我将调用self.collectionView.reloadeItems(位于:[indexPath])以触发用于设置新高度的委托方法 但是,reloadeitems方法似乎将重新创建一个单元,而不仅仅是重新布局一个原始的重用单元 我怎样才能解决这个问题?这是苹果UICollectionView上的bug还是我做错了什么 全部代码: // code from ViewController func coll

我的手机有一个可以从网络下载的图像,因此我需要将手机的高度设置为动态。 图像下载完成后,我将调用
self.collectionView.reloadeItems(位于:[indexPath])
以触发用于设置新高度的委托方法

但是,
reloadeitems
方法似乎将重新创建一个单元,而不仅仅是重新布局一个原始的重用单元

我怎样才能解决这个问题?这是苹果UICollectionView上的bug还是我做错了什么

全部代码:

// code from ViewController
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: AnnounmentWallCollectionViewCell.cellIdentifier, for: indexPath) as! AnnounmentWallCollectionViewCell

    let announcement = announcements[indexPath.row]
    cell.collectionView = collectionView
    cell.setBanner(from: announcement.banner, indexPath: indexPath, completion: { [unowned self] (height) in
        self.bannersHeight[indexPath.row] = height
    })
    cell.setHTMLContent(announcement.content)
    contentsHeight[indexPath.row] = cell.htmlContentSize.height
    printD("indexPath: \(indexPath)")
    return cell
}

// code from cell
func setBanner(from url: URL?, indexPath: IndexPath, completion: @escaping (_ height: CGFloat)->()) {
    // URL(string: "https://i.imgur.com/qzY7BJ9.jpg")
    if let url = url {
        if let banner = SDImageCache.shared().imageFromDiskCache(forKey: url.absoluteString) {
            self.bannerView.isHidden = false
            self.bannerView.image = banner.scaleWidth(to: self.bounds.width - 32) // leading + trailling
            self.bannerHeight.constant = self.bannerView.image?.size.height ?? 1
            completion(self.bannerHeight.constant)
            printD("NO Download: \(indexPath)")
            let animationsEnabled = UIView.areAnimationsEnabled
            UIView.setAnimationsEnabled(false)
            self.collectionView.reloadItems(at: [indexPath])
            UIView.setAnimationsEnabled(animationsEnabled)
        } else {
            DispatchQueue.global().async {
                SDWebImageDownloader.shared().downloadImage(with: url, options: .useNSURLCache, progress: nil) { (banner, data, error, finished) in
                    DispatchQueue.main.async {
                        if let banner = banner {
                            SDImageCache.shared().store(banner, forKey: url.absoluteString, toDisk: true)
                            self.bannerView.isHidden = false
                            self.bannerHeight.constant = banner.scaleWidth(to: self.bounds.width - 32)?.size.height ?? 1
                            completion(self.bannerHeight.constant)
                            self.collectionView.reloadData()
                            printD("Download: \(indexPath): \(self.bannerHeight.constant)")
                        } else {
                            self.bannerView.isHidden = true
                            self.bannerHeight.constant = 1
                            completion(self.bannerHeight.constant)
                        }
                    }
                }
            }
        }
    } else {
        bannerView.isHidden = true
        bannerHeight.constant = 1
        completion(bannerHeight.constant)
    }
}

// code from delegate 
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    let width = self.view.bounds.width
    let height = bannersHeight[indexPath.row] + contentsHeight[indexPath.row]
        + 1 // sticker
        + 11 // banner top
    printD("indexPath: \(indexPath): \(height)")
    return CGSize(width: width, height: height)
}

那不是虫子。这就是为给定索引路径重新加载单元格的方式。如果您只想更新布局,也可以尝试

[self.collectionView.collectionViewLayout invalidateLayout]
而不是

self.collectionView.reloadItems(at: [indexPath])
然后在委托方法中返回正确的大小

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
return //whatever size that you want to return. 
}


我还强烈建议您缓存您的图像大小,以便下次使用,而不是下载/计算超过

谢谢。顺便说一句,我听你建议我缓存调整大小的图像而不是原始图像,并从缓存图像中获取高度。但我似乎从SDImageCache中获取了一个具有错误缩放属性的图像。我将为这个问题打开另一个问题。