背景:
在我的 viewDidLoad 中,我正在将测试日历数据(通过 EventKit)加载到 iPhone。我注意到,当 TableView 第一次加载时,它看不到这些数据。
当我查看日志消息序列时,我发现它是这样的:
在这里记录:
[AppointmentListController viewDidLoad] Starting
[AppointmentListController populateTestData] Populating the test data now
[AppointmentListController tableView:numberOfRowsInSection:] Number of rows = 0
[AppointmentListController viewDidLoad] Loading the existing calendar events
[AppointmentListController viewDidLoad] Ending
问题
为什么“numberOrRowsInSection”在“viewDidLoad”的中途被调用?我问这个是因为这就是我所看到的(这是应该发生的事情吗?)。
如果您不能确定 viewDidLoad 是否会在 numberOrRowsInSection 之前完成是正常的,那么这意味着您需要在“viewDidLoad”方法的末尾使用“[self.tableView reloadData]”来确保它会更新吗?
最佳答案
tableView:numberOfRowsInSection:
方法很可能被调用,因为您在 TableView 上配置了一些东西(可能您已经将它添加到它的 super View )并且因此它需要知道它的指标。它不会突然神奇地自行触发。要么确保在配置 UI 元素之前准备好数据,要么在接近结束时调用 reloadData
,但我发现前者更“干净”。
关于iphone - 为什么 "numberOrRowsInSection"在 "viewDidLoad"中途被调用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5361030/