gpt4 book ai didi

ios - 在 tableview 单元格中使用 KVO 跟踪对 Swift 3 中类实例的特定属性的更改

转载 作者:行者123 更新时间:2023-11-29 11:42:16 26 4
gpt4 key购买 nike

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

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

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

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

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

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

最佳答案

我建议简化添加/删除观察者逻辑。当前的代码太复杂,提供的路径太多,您可能会错过一条或另一条。所以,只需在 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 不会被调用,因此您必须手动添加在这种情况下,观察者自己。

关于ios - 在 tableview 单元格中使用 KVO 跟踪对 Swift 3 中类实例的特定属性的更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45805868/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com