ios - 在 UICollectionView 中显示进度

标签 ios swift uicollectionview

我正在尝试在 UICollectionView 单元格中显示下载进度。当我重复使用单元格时,它有一个错误,当我滚动它时,进度出现在错误的单元格上。

我引用了这个链接,我只是这样做:Displaying download progress in reusable cells

我就是这样做的。我创建了一个模型:

class NewsFeedDownload{
    var index: Int?
    var uniqueId: String?
    var cell: UICollectionViewCell?
    var progressBar: CircularProgressBar?
    var progressContainerView: UIView?
    var backgroundView: UIView?
} 

我有变量来根据唯一键存储此模型数据:

var downloadList        = [String: NewsFeedDownload]()

我正在 willDisplay cell 中初始化它(也在 cellForItemAt 中尝试过),如下所示:

if let uniqueId = self.arObjectList[indexPath.item].id{

    currentCell.downloadBackgroundView.alpha    = 0.0
    currentCell.progressBarContainer.alpha = 0.0
    if !self.downloadList.contains(where: { $0.key == uniqueId }) {
        print("adding object in downloadList : \(indexPath.item, uniqueId)")
        let downloadObject                   = NewsFeedDownload()
        downloadObject.index                 = indexPath.item
        downloadObject.uniqueId              = uniqueId
        downloadObject.cell                  = currentCell
        let progress                         = CircularProgressBar.init(frame: currentCell.progressBarContainer.bounds)
        currentCell.progressBarContainer.addSubview(progress)
        downloadObject.progressContainerView = currentCell.progressBarContainer
        downloadObject.progressBar           = progress
        downloadObject.backgroundView        = currentCell.downloadBackgroundView
        self.downloadList[uniqueId]               = downloadObject

    }
}

我有一个进度回调,我根据键获取模型对象并显示该对象的进度:

func downloadProgress(progress: Double, id: String){
   let downloadObect = downloadList[id]
   downloadObect?.progressBar?.showProgress(percent: Float(progress * 100))
   downloadObect?.backgroundView?.alpha = 1.0
   downloadObect?.progressContainerView?.alpha = 1.0
}

我有一个函数可以检查是否有任何下载。我在 cellForItem 方法中调用此函数:

func checkIfDownloading(){
    for (key, _) in downloadList{
        if VideoDownloader.isDownloading(id: key){
            let downloadObect = downloadList[key]
            if let downloadingCell = downloadObect?.cell as? BreakingNewsCell{
                downloadingCell.downloadBackgroundView?.alpha = 1.0
                downloadingCell.progressBarContainer.alpha = 1.0
                downloadObect?.progressBar?.showProgress(percent: Float(VideoDownloader.getProgressFor(id: key) * 100))
            }else if let downloadingCell = downloadObect?.cell as? NormalNews{
                downloadingCell.downloadBackgroundView?.alpha = 1.0
                downloadingCell.progressBarContainer.alpha = 1.0
                downloadObect?.progressBar?.showProgress(percent: Float(VideoDownloader.getProgressFor(id: key) * 100))
            }

        }else{
            let downloadObect = downloadList[key]
            if let downloadingCell = downloadObect?.cell as? BreakingNewsCell{
                downloadingCell.downloadBackgroundView?.alpha = 0.0
                downloadingCell.progressBarContainer.alpha = 0.0
            }else if let downloadingCell = downloadObect?.cell as? NormalNews{
                downloadingCell.downloadBackgroundView?.alpha = 0.0
                downloadingCell.progressBarContainer.alpha = 0.0
            }
        }
    }
}

在上面的函数中,我显示或隐藏进度条。

我在这方面做错了什么?我该如何修复这个错误?

最佳答案

仅通过 checkIfDownloading 方法获取下载进度的状态。

设置单元格 subview 的状态(在您的情况下为 downloadBackgroundView?.alpha ,progressBarContainer.alpha ,progressBar?.showProgress ) 在 cellForItem 方法中。

关于ios - 在 UICollectionView 中显示进度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49528395/

相关文章:

ios - 点击按钮时仅将 subview 添加到一个 UICollectionViewCell

ios - 如何隐藏 map (或使其统一颜色),同时将图钉保持在顶部?

ios - 如何在我的 iOS 应用程序中播放广播? Swift 和 AVFoundation

ios - Swift - RootPageViewController,添加来自不同 ViewController 的按钮

Swift 2/iOS 9 - 找不到 libz.dylib

ios - 调整 UICollectionViewCell 的大小

ios - 如何将 Apple 的 CircleLayout 的布局逻辑向左旋转 90 度?

ios - 获取 WKInterfaceObject 类的属性

javascript - .click() 不适用于单选按钮

ios - 快速获取错误 : "Missing Argument for Parameter ‘frame’ in call"的核心图