Refresh UIPageViewController - reorder pages and add new pages

≯℡__Kan透↙ 提交于 2019-11-26 15:19:37

问题


I have a UIPageViewController which I am providing page data for using an implementation of 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?

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.


回答1:


You need to call 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).




回答2:


I found a workaround to force UIPageViewController to forget about cached view controllers of neighboring pages that are currently not displayed:

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:.




回答3:


Here's my complete implementation of @ortwin-gentz's answer in the didFinish delegate method in Swift 2, which works perfectly for me:

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



回答4:


Swift 4 Version for reload of data of pageviewcontroller, to @theory's version.

func pageViewController(_ pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) {
    if !completed { return }
    DispatchQueue.main.async() {
        self.dataSource = nil
        self.dataSource = self
    }
}


来源:https://stackoverflow.com/questions/15325891/refresh-uipageviewcontroller-reorder-pages-and-add-new-pages

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!