Swift:如何将项目分组或合并到一个单元格tableView

问题描述 投票:0回答:1

可能无法正确解释,我已经实现了下载文件机制。完美下载文件,甚至URLSession和进度条也可以有效工作。我正在使用TableView进行显示。图片也附上。

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

我该如何实现?我没有这个主意吗?

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
    }

图像:

Download

ios swift uitableview nsurlsession nsurlsessiondownloadtask
1个回答
0
投票

The result you want

log file for download file

您需要做的是替换代码。

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


}

希望是您要找的那个。干杯

© www.soinside.com 2019 - 2024. All rights reserved.