ios - 下载完成后如何在tableView中设置进度条Swift
问题描述
我使用 Alamofire 作为我的 webServices 和 Alamofire 下载方法来下载 pdf 文件。一切正常,数据即将到来,文件也正确下载。唯一的问题是如何为此设置进度条?
我正在使用带有自定义 Xib 的 tableView(它有progressBar)。
- 我想为所有 pdf 文件下载和数据完成度(如 100%)为每个单元格设置一个进度条。
- 如果由于任何原因暂停,它还会显示进度 10% 已完成并停止。(恢复,取消)仅限。
注意:我在 tableView 中使用静态值打印单元格
视图控制器代码:
import UIKit
import Alamofire
class ViewController: UIViewController {
@IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
tableView.dataSource = self
tableView.delegate = self
let nib = UINib.init(nibName: "DownloadEntryViewCell", bundle: nil)
self.tableView.register(nib, forCellReuseIdentifier: "DownloadEntryViewCell")
}
func webService() {
DataProvider.main.serviceGetAppointmentDetail(Id: AppId ?? 0, callback: {success, result in
do{
if(success){
let decoder = JSONDecoder()
let response = try decoder.decode(AppointmentDetail.self, from: result! as! Data)
self.AppDetailData = response
for firmParam in (self.AppDetailData?.sectionList ?? []) {
for firmItem in firmParam.items! {
if firmItem.actionParamData != nil {
let str = firmItem.actionParamData
let param = str?.components(separatedBy: ":")
let final = param![1].replacingOccurrences(of: "}", with: "")
let fmId = final.components(separatedBy: ",")
let frmId = fmId[0]
self.firmDetails(actionParamData: Int(frmId) ?? 0)
}
//pdf download
if firmItem.actionUrl != nil {
let pdfFilesURL = firmItem.actionUrl ?? ""
self.downloadFile(url: pdfFilesURL, filetype: ".pdf", callback: { success, response in
if !success || response == nil {
return false
}
return true
})
}
}
}
return true
}else{
return false
}
}catch let error {
print(error as Any)
return false
}
})
}
@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])
}
}
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)
})
}
}
extension ViewController: UITableViewDataSource, UITableViewDelegate {
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 78
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "DownloadEntryViewCell", for: indexPath) as! DownloadEntryViewCell
return cell
}
}
xib代码:
class DownloadEntryViewCell: UITableViewCell {
@IBOutlet weak var rightView: UIView!
@IBOutlet weak var leftView: UIView!
@IBOutlet weak var fileNameLabel: UILabel!
@IBOutlet weak var downloadURLLabel: UILabel!
@IBOutlet weak var progressLabel: UILabel!
@IBOutlet weak var individualProgress: UIProgressView!
@IBOutlet weak var imgView: UIImageView!
@IBOutlet weak var stackViewFooter: UIStackView!
override func awakeFromNib() {
super.awakeFromNib()
}
}
解决方案
推荐阅读
- django - 使用 Django 的 Case/When 查询集条件来设置多个值?
- java - 实现通知 INSERTS/UPDATES 的回调机制
- docker - 使用 Grafana 访问远程 redis 服务器
- flutter - 有什么方法可以验证底页中的文本字段吗?
- java - 如何使用 Gson 将简单对象转换为字符串?
- sap-business-technology-platform - 通过 OData API 从 SAP ByD 获取员工任务列表
- objectbox - 对象框 DbMaxReadersExceededException
- navigation - 如何在导航菜单中突出显示当前页面?我正在使用带有侧边器的下一个链接和 antd 布局组件
- c# - 如何将日期转换为毫秒(UTC),反之亦然?
- javascript - Nodejs异步循环函数返回空白