为什么“numberOrRowsInSection”是在“viewDidLoad”中途被调用?
背景:
在我的 viewDidLoad 中,我正在将测试日历数据(通过 EventKit)加载到 iPhone。我注意到,当表视图第一次加载时,它看不到此数据。
当我查看日志消息序列时,我发现它是这样的:
在这里记录:
[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
问题(S)
为什么“numberOrRowsInSection”在“viewDidLoad”的中途被调用?我问这个问题是因为这就是我所看到的(这是应该发生的情况吗?)。
如果这是正常现象,您无法确定 viewDidLoad 会在 numberOrRowsInSection 之前完成,那么这意味着您需要在“viewDidLoad”方法末尾添加“[self.tableView reloadData]”以确保它确实更新?
BACKGROUND:
Within my viewDidLoad I am loading test calendar data (via EventKit) to the iPhone. I am noting that when the table view loads the first time it does not see this data.
When I look at the sequence of log messages I see that it goes something like this:
Logging here:
[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
QUESTION(S)
Why does "numberOrRowsInSection" get called mid-way through "viewDidLoad"? I ask this as this is what I am seeing (is this what should happen?).
If this is normal that you can't be sure viewDidLoad will complete before numberOrRowsInSection, then this would imply you need a "[self.tableView reloadData]" at the end of your "viewDidLoad" method to make sure it does update?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
tableView:numberOfRowsInSection:
方法可能会被调用,因为您在表视图上配置了某些内容(也许您已将其添加到其超级视图中),因此它需要了解其指标。它不会突然神奇地自行触发。确保在配置 UI 元素之前准备好数据,或者在接近尾声时调用reloadData
,但我发现前者“更干净”。The
tableView:numberOfRowsInSection:
method is likely called because you configured something on your table view (maybed you've added it to its superview) and it therefor needed to know its metrics. It does not magically trigger by itself out of the blue. Either make sure to prepare your data before you configure the UI elements or callreloadData
near the end, but I'd find the former to be "cleaner".这是完全正常的……
;在 viewdidload 结束时
This is perfectly normal....
; at the end of viewdidload