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