ios - 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")
}
解决方案
几年前我创建了一个示例应用程序来演示这种精确的技术。它并不完美:
https://github.com/chefnobody/Progress
我的猜测是您没有正确建模每次下载的进度。将数据(以及任何异步请求)的建模与表格视图单元格的呈现分开是很重要的,因为 iOS 将在每个单元格滚动到屏幕外时设置和拆除它。您可能还会遇到一些线程问题。
我会复习UITableViewCell
生命周期事件,并确保您完全了解当单元格滚出屏幕时发生的情况。
推荐阅读
- c# - 在启动某些应用程序而没有窃取焦点时出现问题
- c++ - 没有构造函数“std::thread::thread”的实例与参数列表匹配——参数类型为:(void ())
- python - 使用 Scrapy 遍历页面?urljoin 的问题。功能
- c - 尝试访问指针列表中的指针时出现 C 总线错误,该指针列表是指向指针的结构体
- linux - 尝试使用计数器在某些数字上创建 bash 脚本循环
- ios - 优化保存在 Swift 中的图像文件大小(JPG、PNG)
- reactjs - 在 Typescript 中检测 React 子数组长度
- ruby - 从 GtkEntryCompletion 访问 GtkTreeView
- node.js - 使用 node / postgres 在 Postman 上使用图像测试创建 / 更新 API
- java - 如果 Java 整数文字的默认类型是 int 类型,为什么这些赋值语句会编译?