在表视图单元格中使用 KVO 跟踪对 Swift 3 中类实例的特定属性的更改



我正在尝试使用 KVO 来跟踪对我的 Account 对象的属性的更改,其重要部分如下所示:

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

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

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
}
}

这是我的表视图类的相关部分:

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 类中实现了委托函数来重新加载特定的单元格。

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

尽管我尝试了标志和其他策略来正确跟踪观察者的添加和删除,但我仍然收到"无法删除密钥路径的观察者,因为它未注册为观察者"错误。

我建议简化添加/删除观察器逻辑。当前的代码太复杂了,并且提供了太多的路径,您可能会错过其中一个。因此,只需删除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))
}

此外,如果您在init中设置了account,请记住,当时不会调用willSet,因此您必须在这种情况下自己手动添加观察器。

最新更新