【发布时间】:2017-09-14 01:03:33
【问题描述】:
我尝试的第一种方法由于破坏布局而导致错误,但基本上是我的问题:在有一个表格视图,其单元格都有滑块/进度视图。如果用户点击下载,这些滑块上的值需要等于附加项目(我将 RSS 项目分配给每个单元格,它的音乐)的下载百分比。
我在这里在观察者中调用这个函数来获取下载进度:
//DOWNLOAD PROGRESS
func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didWriteData bytesWritten: Int64, totalBytesWritten: Int64, totalBytesExpectedToWrite: Int64) {
let progress = Double(totalBytesWritten) / Double(totalBytesExpectedToWrite)
print("\(downloadTask.originalRequest!.url!.absoluteString) \(progress)")
updateListCells()
}
这是我尝试更新单元格的方法,以便用户可以看到进度条逐渐增长 - 我对单元格进行了子类化:
func updateListCells()
{
if let table = listVC.myTableView {
if(!table.visibleCells.isEmpty)
{
for v in table.visibleCells {
let cell = v as! RSSTableViewCell
cell.updateLoader()
}
}
}
}
func updateLoader()
{
self.bringSubview(toFront: downloadLoader)
if(pathUrl != nil)
{
if(checkIfExists(url: pathUrl!))
{
downloadLoader.progress = 1
}
else {
for d in downloadManager.operations {
if(URL(string: rssItem.link) == d.value.task.originalRequest?.url)
{
let progress = Double(d.value.task.countOfBytesReceived) / Double(d.value.task.countOfBytesExpectedToReceive)
//print("MATCH: ", d.value.task.countOfBytesSent)
downloadLoader.progress = Float(progress)
}
}
}
}
else {
downloadLoader.progress = 0
}
}
但这不能连续工作。仅当我刷新 tableview 时,滑块才会采用其当前下载值。
如果正在下载,如何持续更新表格视图单元格?
【问题讨论】:
-
不确定,但也许在主线程中运行这个“downloadLoader.progress = Float(progress)”会起作用吗?
-
什么意思?它必须反复设置到滑块,我不会设置一个计时器来一遍又一遍地做这件事
标签: ios swift download tableview nsurlsession