Swift : 如何将下载项目分组或俱乐部化到一个单元格 tableView

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

可能是我没解释清楚,我实现了下载文件机制。文件下载完美,甚至 URLSession 和进度条也能高效工作。我正在使用 TableView 进行显示。图片也附上。

问题 是我想加入或合并到一个下载单元格。一个 AppName 包含 11 个文件,因此它的单元格显示 11 次。但我想显示它只显示一次,并且进度一直持续到文件结束。它应该显示 1 个单元格,而不是 11 个单元格。

我怎样才能做到这一点?我对此一无所知?

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 数据源:

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
    }

图片:

您需要做的是替换您的代码。

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

在 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
}

在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()


}

希望是你正在寻找的那个for.Cheers