Индикатор выполнения не показывает обновленное значение, когда я возвращаюсь с другого контроллера - PullRequest
0 голосов
/ 09 октября 2018

Я хочу показать прогресс загрузки в ячейке табличного представления, но когда я возвращаюсь с другого контроллера, индикатор выполнения не показывает обновленное значение в ячейке.Он покажет только последнее полученное значение.Я пытался обновить значение прогресса, используя NOTIFICATION CENTER и DELEGATE, но оба не работают.Как я могу этого добиться.Пожалуйста, помогите мне.

Вот мой код, который я реализовал в своем проекте.Методы startDownloading внедрены в общий класс моего проекта

func startDownloading(url: String, indexpath: IndexPath) {
if (NetworkReachabilityManager()!.isReachable) {

    let destination = DownloadRequest.suggestedDownloadDestination(for: .documentDirectory)

    Alamofire.download(URL(string: url)!, method: .get, parameters: nil, encoding: JSONEncoding.default, headers: nil, to: destination).downloadProgress(queue: .main) { (progress) in
        DispatchQueue.main.async {
            print("download: \(progress)")
            NotificationCenter.default.post(name: Notification.Name("downloadSong"), object: ["row": indexpath.row, "value": progress])
        }
        }.response(queue: .global(qos: .background)) { (response) in
            if let destinationPath = response.destinationURL {
                NotificationCenter.default.post(name: Notification.Name("downloadSong"), object: indexpath.row)
                NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: "downloadSong"), object: nil)
            }
    }
} else {
    print("Please connect to Internet.")
}
}

и следующий метод реализован в классе UIViewcontroller:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
 let cell = tableView.dequeueReusableCell(withIdentifier:"cellReuseIdentifier") as! AllSongsTableCell
    cell.downloadTapped = {
        NotificationCenter.default.addObserver(self, selector: #selector(self.updateCell(_:)), name: NSNotification.Name("downloadSong"), object: nil)
        startDownloading(url: "song-url", indexpath: indexPath)
    }
}

//Observer Method
@objc func updateCell(_ notification: NSNotification) {
    if let object = notification.object as? [String: Any] {
        DispatchQueue.main.async {
            if let cell = self.allSongsTable.cellForRow(at: IndexPath(row: object["row"] as! Int, section: 0)) as? AllSongsTableCell {
                let value = object["value"] as! Double
                cell.graphView.value = UICircularProgressRing.ProgressValue(value)
            }
            self.allSongsTable.reloadRows(at: [IndexPath(row: object, section: 0)], with: .none)
        }
    }
}
...