啊哈!我發現瞭如何去做! Apple iOS 8 sample code on size classes演示瞭如何做到這一點。
我只在cellForRowAtIndexPath
數據源方法中設置了我的UITableViewCell
實例,當然,每個單元格的創建只調用一次。
Apple的示例代碼配置willDisplayCell
委託方法中的表格單元附件視圖,並且當發生UISplitViewController
轉換時,此方法似乎會在每個可見單元格上自動調用。 :)
編輯:好吧,進一步探索,事實證明它不是自動的。這是一個NSNotification
。
下面是來自Apple示例應用程序的代碼。在視圖控制器的viewDidLoad
:
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(ListTableViewController.showDetailTargetDidChange(_:)), name: NSNotification.Name.UIViewControllerShowDetailTargetDidChange, object: nil)
}
而且方法通知觸發:
func showDetailTargetDidChange(_ notification: Notification) {
/*
Whenever the target for showDetailViewController: changes, update all
of our cells (to ensure they have the right accessory type).
*/
for cell in tableView.visibleCells {
if let indexPath = tableView.indexPath(for: cell) {
tableView(tableView, willDisplay: cell, forRowAt: indexPath)
}
}
}
來源
2016-11-26 23:58:50
TiM
沒有用它自己,但它聽起來像一個[UISplitViewControllerDelegate(HTTPS作業://開發商.apple.com /參考/ UIKit的/ uisplitviewcontrollerdelegate)。 – pbasdf
理想情況是,但我已經有了一個父視圖控制器,我已經在使用'委託'(管理所有當前可見的視圖控制器的狀態)。我希望在當前的視圖控制器級別上有另一種方式接收轉換通知。我只是看着'UIViewController'上的' - (void)collapseSecondaryViewController:(UIViewController *)secondaryViewController forSplitViewController:(UISplitViewController *)splitViewController'方法,但這似乎並沒有被調用。 – TiM