在tableview单元格中使用KVO来跟踪Swift 3

时间:2017-08-21 21:35:39

标签: ios swift3 key-value-observing

我尝试使用KVO跟踪对帐户对象属性的更改,其中重要部分如下所示:

class Account: NSObject {
    var battleTagLabel: String!
    dynamic var onlineStatusIcon: String!
    dynamic var currentGameIcon: String!
    dynamic var currentStatusLabel: String!

当这三个属性的值发生变化时,我希望在我的tableview单元格中收到通知。我的tableview单元类:

import Foundation
import UIKit

private var observerContext = 0

class FriendAccountCell: UITableViewCell {

    @IBOutlet weak var onlineStatusIcon: UIImageView!
    @IBOutlet weak var battleTag: UILabel!
    @IBOutlet weak var currentGameIcon: UIImageView!
    @IBOutlet weak var currentStatusLabel: UILabel!

    weak var tableView: UITableView!
    weak var delegate: CustomCellDelegate?
    var onlineStatusIconFlag = false
    var currentStatusLabelFlag = false
    var currentGameIconFlag = false

    var account: Account? {

        willSet {
            if onlineStatusIconFlag {
                print("onlineStatusIconFlag: \(onlineStatusIconFlag)")
                if newValue?.onlineStatusIcon != account?.onlineStatusIcon && account?.onlineStatusIcon != nil {
                    self.account?.removeObserver(self, forKeyPath: #keyPath(onlineStatusIcon))
                    onlineStatusIconFlag = false

                }
            }
            if currentStatusLabelFlag {
                if newValue?.currentStatusLabel != account?.currentStatusLabel && account?.currentStatusLabel != nil {
                    account?.removeObserver(self, forKeyPath: #keyPath(currentStatusLabel))
                    currentStatusLabelFlag = false
                }
            }

            if currentGameIconFlag {
                if newValue?.currentGameIcon != account?.currentGameIcon && account?.currentGameIcon != nil {
                    account?.removeObserver(self, forKeyPath: #keyPath(currentGameIcon))
                    currentGameIconFlag = false

                }
            }
        }

        didSet {
            if oldValue?.onlineStatusIcon != account?.onlineStatusIcon {
                if account?.onlineStatusIcon == "onine" {
                    self.onlineStatusIcon.image = UIImage(named: "20pxButtonGreen")
                } else if account?.onlineStatusIcon == "idle" {
                    self.onlineStatusIcon.image = UIImage(named: "20pxButtonYellow")
                } else if account?.onlineStatusIcon == "busy" {
                    self.onlineStatusIcon.image = UIImage(named: "20pxButtonRed")
                } else {
                    self.onlineStatusIcon.image = UIImage(named: "20pxButtonBlack")
                }
                account?.addObserver(self, forKeyPath: #keyPath(onlineStatusIcon), context: &observerContext)
                onlineStatusIconFlag = true
            }

            if oldValue?.currentStatusLabel != account?.currentStatusLabel {
                self.currentStatusLabel?.text = account?.currentStatusLabel
                account?.addObserver(self, forKeyPath: #keyPath(currentStatusLabel), context: &observerContext)
                currentStatusLabelFlag = true
            }

            if oldValue?.currentGameIcon != account?.currentGameIcon {
                if let currentGame = account?.currentGameIcon {
                    switch currentGame {
                    case "overwatch":
                        self.currentGameIcon.image = UIImage(named: "logo-ow")
                    case "hearthstone":
                        self.currentGameIcon.image = UIImage(named: "logo-hs")
                    case "worldOfWarcraft":
                        self.currentGameIcon.image = UIImage(named: "logo-wow")
                    case "diablo3":
                        self.currentGameIcon.image = UIImage(named: "logo-d3")
                    case "heroesOfTheStorm":
                        self.currentGameIcon.image = UIImage(named: "logo-heroes")
                    case "starCraft":
                        self.currentGameIcon.image = UIImage(named: "logo-sc")
                    case "starCraft2":
                        self.currentGameIcon.image = UIImage(named: "logo-sc2")
                    case "":
                        self.currentGameIcon.image = nil
                    default:
                        self.currentGameIcon.image = nil
                    }
                }
                account?.addObserver(self, forKeyPath: #keyPath(currentGameIcon), context: &observerContext)
                currentGameIconFlag = true
            }
        }

    }
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
    guard context == &observerContext else {
        super.observeValue(forKeyPath: keyPath, of: object, change: change, context: context)
        return
    }
    delegate?.didUpdateObject(cell: self)
}


deinit {
    print("deinit called")
    if onlineStatusIconFlag {
        account?.removeObserver(self, forKeyPath: #keyPath(onlineStatusIcon))
        onlineStatusIconFlag = false
    }
    if currentStatusLabelFlag {
        account?.removeObserver(self, forKeyPath: #keyPath(currentStatusLabel))
        currentStatusLabelFlag = false
    }
    if currentGameIconFlag {
        account?.removeObserver(self, forKeyPath: #keyPath(currentGameIcon))
        currentGameIconFlag = false
    }
}

这是我的tableview类的相关部分:

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let identifier: String = "FriendAccountCell"
    var cell: FriendAccountCell
    if let friendCell = self.tableView.dequeueReusableCell(withIdentifier: identifier){
        cell = friendCell as! FriendAccountCell
    } else {
        cell = FriendAccountCell(style: .default, reuseIdentifier: identifier)
        cell.selectionStyle = .none
    }
    var filteredFriends = orderFriends(friendsArray: Array(MyAccountInfo.allFriends.values))

    cell.delegate = self
    cell.account = filteredFriends[indexPath.row]
    cell.battleTag.text = filteredFriends[indexPath.row].battleTagLabel
    cell.currentStatusLabel.text = filteredFriends[indexPath.row].currentStatusLabel

    return cell
}

(上面没有粘贴,但我也在tableview类中实现了委托函数来重新加载特定的单元格。)

当应用程序首次加载并从服务器获取所有最新数据时,对这些特定属性的更改会很快发生。之后,变化更加稳定而缓慢地发生。

尽管有标志和其他策略我试图正确跟踪观察者的添加和删除,但我仍然得到了#34;无法删除关键路径的观察者,因为它没有注册为观察者"错误。

1 个答案:

答案 0 :(得分:1)

我建议简化添加/删除观察者逻辑。当前代码太复杂,并提供了太多路径,您可能会错过其中一个或另一个。因此,只需删除willSet中的观察者,并在didSet中添加观察者:

var account: Account? {

    willSet {
        account?.removeObserver(self, forKeyPath: #keyPath(Account.onlineStatusIcon))
        account?.removeObserver(self, forKeyPath: #keyPath(Account.currentStatusLabel))
        account?.removeObserver(self, forKeyPath: #keyPath(Account.currentGameIcon))
    }

    didSet {
        account?.addObserver(self, forKeyPath: #keyPath(Account.onlineStatusIcon), context: &observerContext)
        account?.addObserver(self, forKeyPath: #keyPath(Account.currentStatusLabel), context: &observerContext)
        account?.addObserver(self, forKeyPath: #keyPath(Account.currentGameIcon), context: &observerContext)

        // do any additional logic here you want here
    }

}

deinit {
    account?.removeObserver(self, forKeyPath: #keyPath(Account.onlineStatusIcon))
    account?.removeObserver(self, forKeyPath: #keyPath(Account.currentStatusLabel))
    account?.removeObserver(self, forKeyPath: #keyPath(Account.currentGameIcon))
}

此外,如果您在account中设置init,请记住之后未调用willSet,因此您必须在这种情况下自行手动添加观察者。