首页 > 解决方案 > UITableView 滚动后单元格 UIProgressView 不更新

问题描述

在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 想法启发的解决方案 *

再添加一个数组来存储下载单元格 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")
}

标签: iosswiftuitableviewcelluiprogressview

解决方案


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

https://github.com/chefnobody/Progress

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

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


推荐阅读