在UICollectionView中显示进度

时间:2018-03-28 07:20:32

标签: ios swift uicollectionview

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

我引用了这个链接,我只是这样做: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
            }
        }
    }
}

在上面的功能中,我显示或隐藏了进度条。

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

2 个答案:

答案 0 :(得分:0)

仅从checkIfDownloading方法获取Downloading Progress的状态。

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

答案 1 :(得分:0)

我认为如果这些更新来自视图控制器,那将会更好:

  • 在cellForItemAt中,更新单元格的progressView和
    状态(启动,停止,下载)
  • 让VideoDownloader每次启动时通知(通过NSNotification或委托)CollectionViewController 停止下载新闻源以及每次给定新闻源的进度变化。
  • 收到此信息时,CollectionViewController可以调用collectionView reloadItemAt方法来刷新单元格 具体的新闻源。

通过这种方式,您确保在回收单元格时不会进入混合,因为它始终是将更新单元格进度的cellForItemAt方法。