2016-10-21 54 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