首页 > 解决方案 > 下载完成后如何在tableView中设置进度条Swift

问题描述

我使用 Alamofire 作为我的 webServices 和 Alamofire 下载方法来下载 pdf 文件。一切正常,数据即将到来,文件也正确下载。唯一的问题是如何为此设置进度条?

我正在使用带有自定义 Xib 的 tableView(它有progressBar)。

注意:我在 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()

    }

}

xib图片: 西布

标签: iosswiftuitableviewalamofireuiprogressview

解决方案


推荐阅读