UITableView 的選中狀態


一、cell 在初始化或者重用時,調用 -_configureCellForDisplay:forIndexPath: 方法,這個方法會做一些附帶操作,其中會調用 setSelected:animated: 方法,將 cell 的選中狀態置爲未選中。




1 在 tableView:cellForRowAtIndexPath: 中調用 setSelected:animated: 方法將 cell 設置爲了選中

。


2 之後又在 -_configureCellForDisplay:forIndexPath: 中調用了setSelected:animated: 方法,將 cell 設置爲了未選中

。


3 可以通過實現 UITableViewDelegate 的 willDisplayCell:forRowAtIndexPath: 方法,在 cell 即將顯示的時候,對相應 indexPath 的 cell 的選中狀態進行設置,這個方法會在 -_configureCellForDisplay:forIndexPath: 後調用。


UITableView 會在再次顯示到屏幕上時將已選中的 cell 選中狀態置爲未選中,若需要保存選中狀態則需要自己實現,保存選中的 cell 的 IndexPath,並在 viewWillAppear: 中調用 selectRowAtIndexPath:animated:scrollPosition: 方法將保存的 NSIndexPath 數組中對應的 cell 選中。
 


二、參考資料
 http://stackoverflow.com/a/25128477
 http://stackoverflow.com/a/30736675
         https://imciel.com/2016/06/15/select-uitableviewcell-the-right-way/

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章