代码之家  ›  专栏  ›  技术社区  ›  Vinícius Barcelos

从多节UITableView中的UITableViewCell获取IndexPath以响应通知

  •  0
  • Vinícius Barcelos  · 技术社区  · 6 年前

    我有一个包含多个部分的tableView,我想在一个单元格中(通过通知)显示Alamofire正在处理的下载进度。

    现在,我已经有了一个通知贴,作为信息,一个事件对象工作和传递,如下所示:

    let info = ["episode": episode, "progress": progress.fractionCompleted] as [String : Any]
    NotificationCenter.default.post(name: .downloadProgress, object: nil, userInfo: info)
    

    我不知道如何在我的单元格中循环找到哪一个有那个插曲,然后得到它的索引,这样我就可以正确地响应通知了。

    我试图获取作为数据源的数组的索引,但是由于tableView有多个部分,因此无法工作。

    有人能帮我吗?谢谢

    我的TableViewController:

    //
    //  EpisodesViewController.swift
    //  Podee
    //
    //  Created by Vinícius Barcelos on 21/07/18.
    //  Copyright © 2018 Vinícius Barcelos. All rights reserved.
    //
    
    import UIKit
    import RealmSwift
    import Kingfisher
    
    class EpisodesTableViewController: UITableViewController {
    
        //MARK:- Variables
        var episodes: Results<Episode> = RealmService.shared.read(object: Episode.self).sorted(byKeyPath: "pubDate", ascending: true)
        let episodesCellId = "episodesCellId"
        var notificationToken: NotificationToken?
    
        var episodesDictionary = Dictionary<Date, [Episode]>()
        var dateDays = [Date]()
    
        //MARK:- Lifecycle
        override func viewDidLoad() {
            super.viewDidLoad()
            setupTableView()
            setupObservers()
        }
    
        override func viewWillAppear(_ animated: Bool) {
            super.viewWillAppear(animated)
            tableView.reloadData()
        }
    
        deinit {
            self.notificationToken?.invalidate()
            //NotificationCenter.default.removeObserver(self, name: NSNotification.Name.downloadProgress, object: nil)
        }
    
    
        //MARK:- Setup
        fileprivate func setupObservers() {
            NotificationCenter.default.addObserver(self, selector: #selector(handleDownloadProgressNotification(notification:)), name: .downloadProgress, object: nil)
            }
        }
    
        @objc func handleDownloadProgressNotification(notification:Notification) {
            ////////
        }
    
    
        //MARK:- Tableview methods
        override func numberOfSections(in tableView: UITableView) -> Int {
            return episodesDictionary.count
        }
    
        override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            let key = dateDays[section]
            guard let datesValues = episodesDictionary[key] else {
                return 0
            }
            return datesValues.count
        }
    
        override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
            let dateFormatter = DateFormatter()
            dateFormatter.dateFormat = "dd MMMM"
            return dateFormatter.string(from: dateDays[section])
        }
    
        override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            let cell = tableView.dequeueReusableCell(withIdentifier: episodesCellId, for: indexPath) as! EpisodesTableViewCell
    
            let key = dateDays[indexPath.section]
    
            if let podcastValues = episodesDictionary[key] {
                cell.delegate = self
                cell.progressBar.isHidden = true
                cell.episode = podcastValues[indexPath.row]
            }
            return cell
        }
    
    }
    

    // Start download
            Alamofire.request(episode.streamURL).downloadProgress { (progress) in
            // Send a notification about the download progress
            let info = ["episode": episode, "progress": progress.fractionCompleted] as [String : Any]
            NotificationCenter.default.post(name: .downloadProgress, object: nil, userInfo: info)
            //print(progress)
            // Check data
        }.responseData { (responseData) in ......
    
    1 回复  |  直到 6 年前
        1
  •  0
  •   Sahil Manchanda    6 年前

    修改下载功能并添加以下参数

     func downloadFile(url: String,date: Date, index: Int){
            let utilityQueue = DispatchQueue.global(qos: .utility)
            Alamofire.download(url)
                .downloadProgress(queue: utilityQueue) { progress in
                    let info: [String: AnyHashable] = ["date": date,
                                 "index" : index,
                                 "progress": progress.fractionCompleted
                    ]
                    NotificationCenter.default.post(name: .downloadProgress, object: nil, userInfo: info)
                }
                .responseData { response in
                  ......  
            }
        }
    

    在viewcontroller中,将函数替换为以下代码:

    @objc func handleDownloadProgressNotification(notification:Notification) {
            var dateDays = [Date]()
            guard let info = notification.userInfo,
            let date = info["date"] as? Date,
            let index = info["index"] as? Int,
            let progress = info["progress"] as? Double,
            let section = dateDays.index(where: {$0 == date})
            else {return}
    
            let indexPath = IndexPath(item: index, section: section)
    
        }