好的,我查看了Displaying download progress in reusable cells,但在重新加载表后,使用URL Session跟踪tableview单元格的下载进度时遇到了问题:
func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didWriteData bytesWritten: Int64, totalBytesWritten: Int64, totalBytesExpectedToWrite: Int64) {
if totalBytesExpectedToWrite > 0 {
let progress = Float(Double(totalBytesWritten) / Double(totalBytesExpectedToWrite))
itemsDownloadingNow[(downloadTask.originalRequest?.url)!] = progress
//update
for (i, prog) in itemsDownloadingNow
{
if(i.lastPathComponent == episode?.enclosure?.url.lastPathComponent)
{
//is downloading
circleProgress?.percentage = prog
}
}
}
}
这是我迄今为止尝试过的(这是在TableViewCell类中),通过URL在全局字典中存储每个任务的进度:var itemsDownloadingNow = [URL: Float]()
如果表没有重新加载(单元格被洗牌),则可以正常工作,但是重新加载圆形进度后会回到零/不跟踪。我不知道如何正确地执行此操作,并且不断更新圈子的唯一方法是将其放入此URL会话功能中。
如何跟踪每个单元格的下载进度?