ios - 具有动态数据源页面的 UIPageViewController 被重复

标签 ios swift 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 中设置所有的 View Controller ,因为它可能有数百个......

最佳答案

最愚蠢的解决方案是向 ProfileViewController 添加一个 index。如果这样做,您可以将其设置为零到第一页。每当您被要求提供下一个或上一个 View Controller 时,您总是相对地知道索引是什么,因为您可以从给定的 pageViewController(实际上是您的 ProfileViewController).

否则 currentProfileIndex 的处理可能很容易出错。

更新

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

关于ios - 具有动态数据源页面的 UIPageViewController 被重复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40179099/

相关文章:

ios - UIPageViewController - 双击点区域显示部分过渡(Xcode 8、Swift 3)

ios - 在 UIPageViewController 中,何时调用 "pageViewController:viewControllerBeforeViewController"?

ios - 啊!不允许 ARC 错误 :'CFTypeRef' (又名 'const void *' )到 'NSNumber *'

ios - 正确显示肖像和风景照片

ios - 将 NSObject 转换为 RLMObject 以将其保存到 Realm 数据库

ios - 在 GIT 上提交 Crashlytics 框架

ios - CGPath 复制 lineJoin 和 miterLimit 没有明显影响

ios - XCode 8.3.3 上的 "Show iteration count"未在相应文件上显示指示

ios - 如何使用 Swift 在 Firebase 中查询最近的用户?

ios - 从另一个 View Controller 更新 UIPageControl