Making a download page in APP, and there are over 10 UIProgressView in the UITableView. The progress bars work fine, but once they are scrolled outside the screen and scrolled back, they update once, then they refuse to update more though they are still downloading.
Here is the code (clear some other code for a clean look):
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)
}
I think it's something about cell reusing, anyone can help?
* Solution inspired by Aaron's idea *
Add one more array to store downloading cell 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")
}
I created a sample app a few years ago that demonstrates this exact technique. It is not perfect:
https://github.com/chefnobody/Progress
My guess is that you're not correctly modeling the progress of each download. It is important to keep the modeling of that data (and any async requests) separate from the rendering of the table view cells because iOS will set up and tear down each cell as it scrolls off screen. You may also have some threading issues going on, as well.
I would brush up on the UITableViewCell
life cycle events and ensure that you fully understand what's happening when a cell scrolls off screen.