0
我知道這可能不是MVC的最佳實踐,但我有一個觀察者在我的自定義tableViewCell中以瞭解何時擴展單元格或不在(代碼如下)。當我按下導航欄上的後退按鈕時,應用程序崩潰,「一個實例TableViewCell被釋放,而關鍵值觀察者仍然註冊它。」如何檢查單元格是否正在觀察,以及如何在用戶點擊後退按鈕時刪除觀察者?非常感謝!!!如何從tableViewCell中移除KVO觀察者?
class ClientDetailTableViewCell: UITableViewCell {
// Sets default and expanded cell heights
class var expandedHeight:CGFloat { get { return 150 } }
class var defaultHeight:CGFloat { get { return 50 } }
// sets to check if a row has been clicked
var frameAdded = false
// checks if height to see if buttons are hidden
func checkHeight() {
button1.hidden = (frame.size.height < PersonTableViewCell.expandedHeight)
}
// Start Key Value Observing
func watchFrameChanges() {
if(!frameAdded) {
addObserver(self, forKeyPath: "frame", options: NSKeyValueObservingOptions.New, context: nil)
frameAdded = true
}
}
// Stop Key Value Observing Changes
func ignoreFrameChanges() {
if(frameAdded) {
removeObserver(self, forKeyPath: "frame")
frameAdded = false
}
}
// If the observer adds keypath for "frame", run checkHeight()
override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
if keyPath == "frame" {
checkHeight()
}
}
}
之前,我需要的,感謝您的幫助! – tahoecoop