2016-12-13 5 views
0

私はセル付きテーブルビューを持っており、オブザーバを追加します。 私はwillDisplay上のセルにオブザーバーを追加します。バックナビゲーションのセルからオブザーバを削除します

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) { 
    let cell = cell as! CustomCell 
    cell.watchFrameChanges() 
} 

私はdidEndDisplayingでそれを削除します。

func tableView(_ tableView: UITableView, didEndDisplaying cell: UITableViewCell, forRowAt indexPath: IndexPath) { 
    let cell = cell as! CustomCell 
    cell.unwatchFrameChanges() 
} 

CustomCell方法:

func watchFrameChanges() -> Void { 
    self.addObserver(self, forKeyPath: "frame", options: NSKeyValueObservingOptions.new, context: nil) 
} 

func unwatchFrameChanges() -> Void { 
    if self.observationInfo != nil { 
     self.removeObserver(self, forKeyPath: "frame") 
    } 
} 

を問題は、私は戻って私から移動するときということですこのTableViewを含むViewControllerでは、オブザーバは削除されず、このエラーが発生します。

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'An instance 0x7f892d216800 of class MyProject.CustomCell was deallocated while key value observers were still registered with it. 

戻るときにオブザーバーを正しく削除するにはどうすればよいですか?

答えて

0

この

override func viewWillDisappear(animated: Bool) {  
    super.viewWillDisappear(animated) 
    for row in 0...(array.count-1) { 
     let indexPath = NSIndexPath(forRow: row, inSection: 0) 
     if let cell = tableView.cellForRowAtIndexPath(indexPath) as? CustomCell { 
      cell.unwatchFrameChanges() 
     } 
    } 
} 
を試してみてください
関連する問題