UITableView 滚动后单元格 UIProgressView 不更新

Cell UIProgressView not update after UITableView scrolls

在APP中制作一个下载页面,UITableView中有10多个UIProgressView。进度条工作正常,但是一旦它们滚动到屏幕外并向后滚动,它们会更新一次,然后他们拒绝更新更多,尽管他们仍在下载。

这是代码(清除一些其他代码以获得干净的外观):

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{

    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") as! DownloadCell

    cell.dnProgress.setProgress(pg, animated: true)
    cell.dnProgress.isHidden = false
    return cell
}

 override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    for v in (cell.subviews){
         if let p = v as? UIProgressView{
            self.downloadingProgress.append(p)
         }
    }
    goDownload(cloudData[indexPath.row)
}

 func updateProgress(_ theData:String, percent:Float){
        let i:Int = downloadingArray.index(of: theData)
        self.downloadingProgress[i].setProgress(percent, animated: true)
    }

我认为这是关于细胞再利用的问题,有人可以帮忙吗?

* 方案灵感来自Aaron的idea *

再增加一个数组来存放下载单元格indexPath

 func updateProgress(_ theData:String, percent:Float){
     let i:Int = downloadingArray.index(of: theData)
     let indexPath = dndingIndexPathArr[i]
     if let cell = tableView.cellForRow(at: indexPath) as? DownloadCell{
        cell.dnProgress.setProgress(percent, animated: true)
    }
    saveData.set(percent, forKey: "\(theData)Progress")
}

几年前我创建了一个示例应用程序来演示这种技术。它并不完美:

https://github.com/chefnobody/Progress

我的猜测是您没有正确模拟每次下载的进度。重要的是将该数据(和任何异步请求)的建模与 table 视图单元格的呈现分开,因为 iOS 将在每个单元格滚出屏幕时设置和拆除它。您可能还会遇到一些线程问题。

我会重温 UITableViewCell 生命周期事件,并确保您完全理解当单元格滚出屏幕时发生的情况。