我有一个基于tableviews创建的微积分视频应用程序,我正在尝试添加离线保存视频文件的功能。我理解我想要实现的目标,但是我很难将进度条添加到特定的单元格中:
目前,通过单击附件按钮开始下载。我有以下方法
override func tableView(tableView: UITableView, accessoryButtonTappedForRowWithIndexPath indexPath: NSIndexPath) {
//Code to save Video to Documents directory goes here
let currentVideo = videos[indexPath.section][indexPath.row]
guard currentVideo.saved == false else {
print("Video is already saved")
return
}
guard let url = currentVideo.url else {
print("Video not found...url is invalid")
return
}
guard currentVideo.downloading == false else {
print("Video is already downloading")
return
}
let session = NSURLSession(configuration: NSURLSessionConfiguration.defaultSessionConfiguration(),
delegate: self,
delegateQueue: NSOperationQueue.mainQueue())
let downloadTask = session.downloadTaskWithURL(url)
downloadTask.resume()
}
现在,我正在实施NSURLSessionDownloadDelegate方法,相关的方法在下面
func URLSession(session: NSURLSession, downloadTask: NSURLSessionDownloadTask, didWriteData bytesWritten: Int64, totalBytesWritten: Int64, totalBytesExpectedToWrite: Int64) {
let progress = Float(totalBytesWritten) / Float(totalBytesExpectedToWrite)
print(progress) //this works and shows progress
}
现在,我想要做的就是更新属性
currentVideo.progress = progress
//where currentVideo is the video for the cell that was tapped
问题是我不知道如何获取此委托方法中的当前视频。我试图以某种方式弄清楚如何使用downloadTask.taskIdentifier或类似的东西,但我无法搞清楚。有人可以指出我正确的方向吗?
答案 0 :(得分:2)
您可以按照以下方式尝试。
在您的班级
下声明全局变量var selectedIndex:NSIndexPath!
然后在accessoryButtonTappedForRowWithIndexPath
方法
selectedIndex = indexPath
现在,在委托方法downloadTask
中分配值
let currentVideo = videos[selectedIndex.section][selectedIndex.row]
currentVideo = // Your value