iosswiftuitableviewnsurlsessionnsurlsessiondownloadtask

Swift : How can I group or club download items to one cell tableView


May be I cannot properly explained it, I have implemented download files mechanism. Files downloading perfectly even URLSession and progress bar works efficiently. I am using TableView for displaying. Image also attached.

Issue is I want to club or merge to one download cell. One AppName contains 11 files so it cell displays 11 times. But I want to show it display only once and progress continues until end of files. Instead of 11 cells it should display 1 cell.

How can I achieve this? I am not getting an idea for this?

fileprivate var fileDownLoadDataArray:[DownLoadData] = []

func detailData(AppId: Int, AppName: String){

        if let url = Bundle.main.url(forResource: "AppointmentDetail", withExtension: "json") {
            do {

                let data = try Data(contentsOf: url)
                let decoder = JSONDecoder()
                let jsonData = try decoder.decode(AppointmentDetail.self, from: data)
                self.AppDetailData = jsonData

                for param in AppDetailData?.sectionList ?? [] {
                    for item in param.items! {

                        if item.actionType == 2 {
                            let filename = item.actionUrl ?? ""

                            let data = DownLoadData(with: AppName, and: item.actionUrl ?? "")
                            fileDownLoadDataArray.append(data)
                        }
                    }
                }

            } catch {
                print("error:\(error)")
            }
        }

    }

Class NSObject


class DownLoadData: NSObject {
    var fileTitle: String = ""
    var downloadSource: String = ""
    var downloadTask: URLSessionDownloadTask?
    var taskResumeData: Data?
    var downloadProgress: Float = 0.0
    var isDownloading: Bool = false
    var isDownloadComplete: Bool = false
    var taskIdentifier: Int = 0
    var groupDownloadON:Bool = false
    var groupStopDownloadON:Bool = false

    init(with title:String, and source:String){
        self.fileTitle = title
        self.downloadSource = source
        super.init()

    }

TableView DataSource:

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return fileDownLoadDataArray.count 
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "ChildTableViewCell", for: indexPath) as! ChildTableViewCell

        let downloadData = fileDownLoadDataArray[indexPath.row]
        cell.configureCell(with: downloadData)
        cell.cellDelegate = self

        return cell
    }

Image:

Download


Solution

  • The result you want

    log file for download file

    What you need to do is to replace your code.

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.Appdata!.count //Appdata?.count ?? 0
    }
    

    In cellForRowAt ..

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "ChildTableViewCell", for: indexPath) as! ChildTableViewCell
        //let dic = Appdata?[indexPath.row]
    
        //cell.fileNameLabel.text = dic?.projectName
    
        let downloadData = fileDownLoadDataArray[indexPath.row]
    
        let mainProject = self.Appdata![indexPath.row]
        print(mainProject.projectName as Any)
    
        cell.configureCell(with: downloadData , projectName: mainProject)
        cell.cellDelegate = self
    
        return cell
    }
    

    In ConfigureCell

     func configureCell(with downloadInfo:DownLoadData, projectName:Appointment){
    
        // Set the download info into the cell
        self.downloadData = downloadInfo
    
        fileNameLabel.text = projectName.projectName
        if self.downloadData.groupDownloadON {
            startOrPauseDownload()
            // reset flag
            self.downloadData.groupDownloadON = false
        }
        if self.downloadData.groupStopDownloadON{
    
            stopDownload()
            self.downloadData.groupStopDownloadON = false
        }
        updateView()
    
    
    }
    

    Hope is that one you are looking for.Cheers