为什么我离开当前视图控制器并返回后,我的UITableView只显示可用的MGLOfflinePack列表?

时间:2017-01-05 05:20:17

标签: ios mapbox mapbox-gl

我使用Mapbox-iOS-SDK 3.3.7与Swift 3开发了一项功能,可以让用户下载地图供离线使用。初始视图将有一个UITableView,它将提供所有以前下载的MGLOfflinePack的列表。我尽力将Objective-C example that Mapbox provides移植到Swift 3以管理离线包。

不幸的是,我遇到了最初显示表时没有出现脱机包的问题。但是,如果我将另一个控制器推送到NavigationController或以其他方式离开并返回到UITableView,则脱机包列表将按预期显示。

我相信我的通知观察员和KVO根据the documentation正确设置,但无论我尝试什么,tableView(_ tableView: UITableView, numberOfRowsInSection section: Int)总是返回0,直到我离开控制器并返回,大概是因为MGLOfflineStorage.shared ().packs在触发代码时返回nil进行检查。

以下是使用我从前面提到的Obj-C示例中移植的代码的相关部分:

import Mapbox

class OfflineMapTableViewController: UITableViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // observers for offline pack loading
        MGLOfflineStorage.shared().addObserver(self, forKeyPath: "paths", options: .initial, context: nil)
        NotificationCenter.default.addObserver(self, selector: #selector(offlinePackProgressDidChange), name: NSNotification.Name.MGLOfflinePackProgressChanged, object: nil)
    }

    deinit {
        MGLOfflineStorage.shared().removeObserver(self, forKeyPath: "paths")
        NotificationCenter.default.removeObserver(self)
    }

    override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {

        guard let change = change else { return }

        if keyPath == "paths" {
            if let changeKind = change[NSKeyValueChangeKey.kindKey] as? UInt,
                let kind = NSKeyValueChange(rawValue: changeKind) {

                var indexPaths: [IndexPath] = []

                if let indexes = change[NSKeyValueChangeKey.indexesKey] as? NSIndexSet {
                    if indexes.count > 0 {
                        indexes.enumerate({ (index, stop) in
                            indexPaths.append(IndexPath(row: index, section: 0))
                        })
                    }
                }

                switch kind {
                case NSKeyValueChange.insertion:
                    tableView.insertRows(at: indexPaths, with: .automatic)
                    break

                case NSKeyValueChange.removal:
                    tableView.deleteRows(at: indexPaths, with: .automatic)
                    break

                case NSKeyValueChange.replacement:
                    tableView.reloadRows(at: indexPaths, with: .automatic)
                    break

                default:
                    tableView.reloadData()

                    if let packs = MGLOfflineStorage.shared().packs {
                        for pack in packs {
                            if pack.state == .unknown {
                                pack.requestProgress()
                            }
                        }
                    }

                    break
                }
            }
        } else {
            super.observeValue(forKeyPath: keyPath, of: object, change: change, context: context)
        }
    }

    func offlinePackProgressDidChange(notification: NSNotification) {
        guard let pack = notification.object as? MGLOfflinePack else { return }

        guard let index = MGLOfflineStorage.shared().packs?.index(of: pack) else { return }

        let indexPath: IndexPath = IndexPath(row: index, section: 0)

        if let cell = tableView.cellForRow(at: indexPath) {
            updateTableViewCell(cell, atIndexPath: indexPath, forPack: pack)
        }
    }

    // MARK: - Table view data source

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if let packs = MGLOfflineStorage.shared().packs {
            return packs.count
        } else {
            return 0
        }
    }

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

        // Configure the cell...

        if let pack = MGLOfflineStorage.shared().packs?[indexPath.row] {
            updateTableViewCell(cell, atIndexPath: indexPath, forPack: pack)
        }

        return cell
    }

    func updateTableViewCell(_ cell: UITableViewCell, atIndexPath indexPath: IndexPath, forPack pack: MGLOfflinePack) {

        if let userInfo = NSKeyedUnarchiver.unarchiveObject(with: pack.context) as? [String: String] {
            if let name = userInfo["name"] {
                cell.textLabel?.text = name
            }
        }
    }

}

任何人都知道为什么只有当我离开然后返回视图控制器时才会出现离线包列表?

1 个答案:

答案 0 :(得分:0)

尝试将forKeyPath: "paths"更改为forKeyPath: "packs"

MGLOfflineStorage属性的名称为packs