【发布时间】:2020-05-20 06:06:21
【问题描述】:
我已经实现了带有自定义单元格的 tableView,用于带有进度条用于跟踪的约会。 如何观察或跟踪 swift 函数的进度条?实际上,我不知道如何保存进度数据以及如何显示它?我有这样的功能层次结构。
- 约会电话(日期)
- 约会详情()
- firmDetails()
- unitData()
- unitDataHistory()
- unitDataImages()
- 下载PDFTask(pdfURL: String)
所有功能都非常有效地完成,但
downloadPDFTaskfunction 花费很少的时间来处理文件。下载 PDF 正在使用alamofire并且只想跟踪它的进度。
我如何跟踪进度条?
下载PDF任务代码:
@objc public func downloadFile(url:String, filetype: String, callback:@escaping (_ success:Bool, _ result:Any?)->(Bool)) -> Void {
var destination = DownloadRequest.suggestedDownloadDestination(for: .documentDirectory)
if filetype.elementsEqual(".pdf"){
destination = { _, _ in
let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
let downloadFileName = url.filName()
let fileURL = documentsURL.appendingPathComponent("\(downloadFileName).pdf")
return (fileURL, [.removePreviousFile, .createIntermediateDirectories])
}
}
self.request = Alamofire.download(
url,
method: .get,
parameters: nil,
encoding: JSONEncoding.default,
headers: nil,
to: destination).downloadProgress(closure: { (progress) in
print(progress)
print(progress.fractionCompleted)
}).response(completionHandler: { (DefaultDownloadResponse) in
callback(DefaultDownloadResponse.response?.statusCode == 200, DefaultDownloadResponse.destinationURL?.path)
print(DefaultDownloadResponse)
})
}
更新代码和图像:
var downloadProgress : Double = 0.0 {
didSet {
for indexPath in self.tableView.indexPathsForVisibleRows ?? [] {
if let cell = self.tableView.cellForRow(at: indexPath) as? DownloadEntryViewCell {
cell.individualProgress.setProgress(Float(downloadProgress), animated: true) //= "\(downloadProgress)" // do whatever you want here
print(downloadProgress)
}
}
}
}
【问题讨论】:
标签: ios swift uitableview alamofire uiprogressbar