ios 刷新 UIPageViewController - 重新排序页面并添加新页面
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15325891/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Refresh UIPageViewController - reorder pages and add new pages
提问by ssloan
I have a UIPageViewController
which I am providing page data for using an implementation of UIPageControllerDelegate
and UIPageControllerDataSource
.
我有一个UIPageViewController
,我正在为使用UIPageControllerDelegate
and的实现提供页面数据UIPageControllerDataSource
。
It's all working fine, but I want to be able to add items to the page data and reorder the page data.
一切正常,但我希望能够将项目添加到页面数据并重新排序页面数据。
If a user has already got to the last of the pages, and then I add an item, they can't get to the next page because viewControllerAfterViewController:
has already been called. If they scroll back one and then forward two they can get to the new page fine, so the data is setup correctly. How can I tell the UIPageViewController
to refresh its store of what comes next?
如果用户已经到达最后一页,然后我添加了一个项目,他们无法到达下一页,因为viewControllerAfterViewController:
已经被调用。如果他们向后滚动一个然后向前滚动两个他们可以很好地进入新页面,因此数据设置正确。我如何告诉UIPageViewController
刷新它的存储接下来会发生什么?
Similarly I would like to reorder the collection that is backing the page view. But if I do this I'll get the same problem - the page view will think the next page is still what it was last time the current page was loaded.
同样,我想重新排序支持页面视图的集合。但是如果我这样做,我会遇到同样的问题——页面视图会认为下一页仍然是上次加载当前页面时的页面。
I guess I'm looking for something similar to reloadData:
on UITableView
.
我想我正在寻找类似于reloadData:
on 的东西UITableView
。
采纳答案by matt
You need to call setViewControllers:direction:animated:completion:
.
你需要打电话setViewControllers:direction:animated:completion:
。
Also, in iOS 6, watch out if you're using UIPageViewControllerTransitionStyleScroll style, as there is a major caching bug if animated:
is YES (see my discussion here: UIPageViewController navigates to wrong page with Scroll transition style).
此外,在 iOS 6 中,请注意您是否使用 UIPageViewControllerTransitionStyleScroll 样式,因为如果animated:
为 YES ,则存在一个主要的缓存错误(请参阅我的讨论:UIPageViewController 导航到错误的页面,使用 Scroll transition style)。
回答by Ortwin Gentz
I found a workaround to force UIPageViewController
to forget about cached view controllers of neighboring pages that are currently not displayed:
我找到了一种解决方法来强制UIPageViewController
忘记当前未显示的相邻页面的缓存视图控制器:
pageViewController.dataSource = nil;
pageViewController.dataSource = self;
I do this everytime I change the set of pages. Of course this doesn't affect the currently displayed page.
我每次更改页面集时都会这样做。当然这不会影响当前显示的页面。
With this workaround I avoid the caching bug and can still use animated:YES
in setViewControllers:direction:animated:completion:
.
通过这种解决方法,我避免了缓存错误,并且仍然可以animated:YES
在setViewControllers:direction:animated:completion:
.
回答by theory
Here's my complete implementation of @ortwin-gentz's answer in the didFinish delegate method in Swift 2, which works perfectly for me:
这是我在 Swift 2 的 didFinish 委托方法中对@ortwin-gentz 的回答的完整实现,这对我来说非常有用:
func pageViewController(
pageViewController: UIPageViewController,
didFinishAnimating finished: Bool,
previousViewControllers: [UIViewController],
transitionCompleted completed: Bool
) {
if !completed { return }
dispatch_async(dispatch_get_main_queue()) {
pageViewController.dataSource = nil
pageViewController.dataSource = self
}
}
回答by Parion
Swift 4 Version for reload of data of pageviewcontroller, to @theory's version.
Swift 4 版本,用于将 pageviewcontroller 的数据重新加载到 @theory 的版本。
func pageViewController(_ pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) {
if !completed { return }
DispatchQueue.main.async() {
self.dataSource = nil
self.dataSource = self
}
}