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

그들 수백 될 수 있기 때문에 내가

답변

2

벙어리 솔루션은 indexProfileViewController에 추가하는 것입니다 ..있는 viewDidLoad에서보기 컨트롤러의 모든 설정 아니에요 . 그렇게하면 첫 번째 페이지에 0으로 설정할 수 있습니다. 다음 또는 이전보기 컨트롤러를 제공하라는 메시지가 표시 될 때마다 주어진 인덱스와 관련하여 항상 알 수 있습니다. 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'도 param으로 가지고 있습니다. 예를 들어, 10이면 이전 뷰 컨트롤러를 만들 때 9가되어야합니다 (11 이후). –

+0

let profile = pageViewController like Sometring? ProfoileViewController? 나는 그렇게 할 수 없다? – HelloimDarius