获取行数据时,活动指示器显示在表视图中
我正在从 tableview1.row 导航到 tableview2,其中有很多行正在被获取。鉴于加载时间约为 3 秒,我希望在选择 tableview1.row 后导航立即滑入 tableview2,然后在 tableview2 上方显示 UIActivityIndicatorView,同时获取数据,然后在其基础表视图中呈现。请注意,tableview2 实际上是父 UIView 的子视图(而不是父视图是 UITableView)。
我看过这篇文章:活动指示器应该从 UITableView1 导航到 UITableView2 时显示
...它提供了将活动指示器 start 和 stopAnimating 调用添加到 tableview2 的 viewDidLoad 中的说明。
问题是,我不确定上述解决方案如何工作,因为 viewDidLoad 在 tableview2 明显滑入视图之前运行并完成。
另外,我还尝试在 IB 中的 tableview2 上添加活动指示器,并将 IBOutlet 指示器的开始/停止动画代码添加到 viewDidAppear 中。发生的情况是数据获取运行,我可以看到指示器旋转,但在获取结束时,表视图为空。似乎 viewDidAppear 来不及将数据添加到表视图,因为 cellForRowAtIndexPath 等已经触发。
有人可以提出任何建议吗?我很可能在这里遗漏了一些明显的东西(我现在已经快凌晨 5 点了,我觉得我的大脑已经糊涂了)。我应该从 viewDidAppear 重新触发 cellForRowAtIndexPath 等吗?问题是我的表视图是子视图而不是父视图吗?
谢谢
I am navigating from tableview1.row to a tableview2 which has a LOT of rows being fetched. Given the load time is around 3 seconds, I want the navigation to slide into tableview2 as soon as the tableview1.row is selected, and then display a UIActivityIndicatorView above tableview2 whilst the data is fetched and then rendered in its underlying table view. Note, tableview2 is actually a subview of the parent UIView (as opposed to the parent being a UITableView).
I've seen this post: Activity indicator should be displayed when navigating from UITableView1 to UITableView2
... which gives instructions to add the activity indicator start and stopAnimating calls around the data fetch into viewDidLoad of tableview2.
Thing is, I'm not sure how the above solution could work as viewDidLoad runs and completes before tableview2 visibly slides into view.
Separately, I also tried adding an activity indicator over tableview2 in IB and added the IBOutlet indicator's start/stop animating code into viewDidAppear. What happens is the data fetch runs and I can see the indicator spinning but at the end of the fetch, the table view is empty. Seems like viewDidAppear is too late to add data to the table view as cellForRowAtIndexPath etc has already fired.
Can anyone please suggest any pointers? I could very well be missing something obvious here (its nearly 5am where I am and think my brain is mush). Should I re-trigger cellForRowAtIndexPath etc from viewDidAppear? Is the issue that my table view is a subview and not the parent view?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我又看了看自己的问题,答案实际上非常简单。任何非常密集的处理通常不应在主线程上运行。我试图在同一线程上进行数据密集型获取和活动指示器的 UI 显示,因此这些数据是按顺序处理的,一个接一个。
解决方案是输入我的新视图控制器,a) 在主线程上启动活动指示器动画,然后 b) 在另一个线程上运行数据密集型获取(使用 PerformSelectorInBackground)。任何与 UI 相关的更新都无法从后台线程应用。这些应该交回主线程。
已经有很多关于此的帖子(例如此处 ),我很遗憾没有从一开始就注意到这些:)
I took another look at my own question and the answer is actually pretty straight forward. Any very intensive processing should generally not be run on the main thread. I was attempting to do my data intensive fetch AND UI display of the activity indicator on the same thread and so these were being processed in sequence, one after the other.
Solution is to enter my new view controller and a) kick off the activity indicator animation on the main thread and then b) run the data intensive fetch on another thread (using performSelectorInBackground). Any UI related updates cannot be applied from the background thread. These should be handed back to the main thread.
Plenty of SO posts on this already (example here), my bad for not picking up on these from the start :)