代码之家  ›  专栏  ›  技术社区  ›  jdleung

uitableview滚动后单元格uiprogressview不更新

  •  1
  • jdleung  · 技术社区  · 6 年前

    在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)
        }
    

    我想这是关于细胞再利用的事,有人能帮忙吗?

    *以亚伦的想法为灵感的解决方案*

    再添加一个数组以存储下载的单元格索引xpath

     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")
    }
    
    1 回复  |  直到 6 年前
        1
  •  0
  •   Aaron    6 年前

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

    https://github.com/chefnobody/Progress

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

    我会好好复习一下 UITableViewCell 生命周期事件并确保您完全理解当一个单元格滚动离开屏幕时发生的事情。