Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/96.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 刷新UIPageViewController-重新排序页面并添加新页面_Ios_Iphone_Cocoa Touch_Uikit_Uipageviewcontroller - Fatal编程技术网

Ios 刷新UIPageViewController-重新排序页面并添加新页面

Ios 刷新UIPageViewController-重新排序页面并添加新页面,ios,iphone,cocoa-touch,uikit,uipageviewcontroller,Ios,Iphone,Cocoa Touch,Uikit,Uipageviewcontroller,我有一个UIPageViewController,我正在为使用UIPageControllerDelegate和UIPageControllerDataSource的实现提供页面数据 一切正常,但我希望能够向页面数据中添加项目并对页面数据重新排序 如果用户已经访问了最后一个页面,然后我添加了一个项目,则他们无法访问下一个页面,因为已经调用了viewControllerAfterViewController:。如果他们向后滚动一页,然后向前滚动两页,他们可以很好地进入新页面,因此数据设置正确。如何

我有一个
UIPageViewController
,我正在为使用
UIPageControllerDelegate
UIPageControllerDataSource
的实现提供页面数据

一切正常,但我希望能够向页面数据中添加项目并对页面数据重新排序

如果用户已经访问了最后一个页面,然后我添加了一个项目,则他们无法访问下一个页面,因为已经调用了
viewControllerAfterViewController:
。如果他们向后滚动一页,然后向前滚动两页,他们可以很好地进入新页面,因此数据设置正确。如何让
UIPageViewController
刷新其存储,以了解接下来会发生什么

类似地,我想对支持页面视图的集合重新排序。但是如果我这样做,我会遇到同样的问题-页面视图会认为下一个页面仍然是上次加载当前页面时的页面


我想我正在寻找类似于
UITableView上的
reloadData:

的东西,您需要调用
setViewControllers:direction:animated:completion:


另外,在iOS 6中,如果您使用的是UIPageViewControllerTransitionStyleScroll样式,请注意,如果
动画:
为“是”,则会出现一个主要的缓存错误(请参阅此处的讨论:).

我找到了一种解决方法,可以强制
UIPageViewController
忽略当前未显示的相邻页面的缓存视图控制器:

pageViewController.dataSource = nil;
pageViewController.dataSource = self;
每次更改页面集时,我都会这样做。当然,这不会影响当前显示的页面


通过这种解决方法,我避免了缓存错误,并且仍然可以在
setViewControllers:direction:animated:completion:
中使用
animated:YES
,这是我在Swift 2中完整实现@ortwin gentz在didFinish委托方法中的答案,非常适合我:

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
    }
}

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
    }
}

谢谢-SetViewController:方向:动画:完成:完成了。也为提到虫子而干杯。很有趣。在哪里取消设置和重置数据源?在
之前设置视图控制器:方向:动画:完成:
,在它之后,还是在完成块中?谢谢。事实上,我在dispatch_async块中完成0.01秒后完成此操作。谢谢!此方法在使用数据源时有效。被接受的答案对我不起作用,至少在iOS 8上是这样。使用“SetViewController:direction:animated:completion:”和当前视图控制器不会导致再次调用数据源。此解决方案的Thx:-)这不起作用,至少在iOS 10.3中不起作用。崩溃发生在setViewControllers()中,因此在0.01秒后执行某些操作太迟。我在调用setViewController()之前尝试过这个技巧,但它不起作用。效果很好…完美..谢谢