2016-10-21 8 views
1

私はページ間ですばやくスワイプすると、重複したページを取得することがあります。インデックスカウントが間違っている可能性がありますが、私はすべてを試して、常に同じようにします。動的なDataSourceページを含む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

非常識なソリューションをProfileViewControllerindexを追加することです。そうした場合、最初のページにゼロとして設定することができます。次回または前のView Controllerの提供を依頼されるたびに、いつも比較的インデックスを知っています。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とAfterで何をするのですか? – HelloimDarius

+0

前後には、 'pageViewController:UIPageViewController'もパラメータとしてあります。たとえば、それが10ならば、前のビューコントローラを作成するときには、後で9とする必要があります。 –

+0

let profile = pageViewControllerのようなSometring? ProfoileViewController?私は本当にそうすることはできないのですか? – HelloimDarius

関連する問題