2016-10-21 68 views
1

当我在页面之间快速滑动时,有时会出现重复页面。我的索引计数可能是错误的,但我尝试了一切,始终保持一致。具有动态数据源页面的UIPageViewController会重复执行

DetailedProfileViewController.swift

class DetailedProfileViewController: UIViewController, UIPageViewControllerDataSource, UIPageViewControllerDelegate { 

    var currentProfileIndex : Int? 
    var nextProfileIndex :Int? 
    var data: Main? 

    var mainPageView : UIPageViewController! 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     let profile = ProfileViewController() 
     profile.profile = currentProfile() 
     let arraysOfViews = [profile] 

     mainPageView = UIPageViewController(transitionStyle: .scroll, navigationOrientation: .horizontal, options: [:]) 
     mainPageView.setViewControllers(arraysOfViews, direction: .forward, animated: true, completion: nil) 
     mainPageView.dataSource = self 
     mainPageView.delegate = self 

     addChildViewController(mainPageView) 
     view.addSubview(mainPageView.view) 
     mainPageView.didMove(toParentViewController: self) 

    } 

    func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? { 
     if currentProfileIndex == (data?.filteredProfiles.count)!-1 { return nil } 
     nextProfileIndex = abs((currentProfileIndex! + 1) % (data?.filteredProfiles.count)!) 
     let profile = ProfileViewController() 
     profile.profile = data?.filteredProfiles[nextProfileIndex!] 
     return profile 
    } 

    func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? { 
     if currentProfileIndex == 0 { return nil } 
     nextProfileIndex = abs((currentProfileIndex! - 1) % (data?.filteredProfiles.count)!) 
     let profile = ProfileViewController() 
     profile.profile = data?.filteredProfiles[nextProfileIndex!] 
     return profile 
    } 

    func pageViewController(_ pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) { 
     if completed == true { 
      currentProfileIndex = nextProfileIndex 
     } 
    } 
} 

我没有设置所有视图控制器的viewDidLoad中,因为它可能是数百人..

回答

2

最愚蠢的解决方案是增加一个indexProfileViewController 。如果你这样做,你可以将它设置为零到第一页。并且每当您被要求提供下一个或上一个视图控制器时,您总是会知道相对于哪个索引,因为您可以从给出的pageViewController(实际上是您的ProfileViewController)中提取它。

否则,处理currentProfileIndex可能非常容易出错。

UPDATE

class DetailedProfileViewController: UIViewController, UIPageViewControllerDataSource, UIPageViewControllerDelegate { 

    var data: Main? 

    var mainPageView : UIPageViewController! 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     let profile = ProfileViewController() 
     profile.index = 0 
     profile.profile = currentProfile() 
     let arraysOfViews = [profile] 

     mainPageView = UIPageViewController(transitionStyle: .scroll, navigationOrientation: .horizontal, options: [:]) 
     mainPageView.setViewControllers(arraysOfViews, direction: .forward, animated: true, completion: nil) 
     mainPageView.dataSource = self 
     mainPageView.delegate = self 

     addChildViewController(mainPageView) 
     view.addSubview(mainPageView.view) 
     mainPageView.didMove(toParentViewController: self) 

    } 

    func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? { 
     guard let pagesCount = data?.filteredProfiles.count else { 
      return nil 
     } 

     guard let newIndex = (viewController as? ProfileViewController).index + 1, newIndex < pagesCount else { 
      return nil 
     } 

     let profile = ProfileViewController() 
     profile.profile = data?.filteredProfiles[newIndex] 
     profile.index = newIndex 
     return profile 
    } 

    func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? { 
     guard let pagesCount = data?.filteredProfiles.count else { 
      return nil 
     } 

     guard let newIndex = (viewController as? ProfileViewController).index - 1, newIndex >= 0 else { 
      return nil 
     } 


     let profile = ProfileViewController() 
     profile.profile = data?.filteredProfiles[newIndex!] 
     profile.index = newIndex 
     return profile 
    } 
} 
+0

我没有得到它。我会在第一次设置profile.index = 0,我会在Before and After中做什么? – HelloimDarius

+0

在之前和之后,你还有'pageViewController:UIPageViewController'作为参数。例如,如果是10,那么当你创建一个视图前控制器时,它应该是9,在 - 11之后。 –

+0

Sometring like let profile = pageViewController as? ProfoileViewController?我真的不能这样做? – HelloimDarius