为每个数组的页面控制器生成动态视图控制器。

问题描述 投票:0回答:0

我在表格视图中有一个项目列表(Cell: Title, Amount),但我想使用页面控制器视图在页面上显示每个项目(Cell: Title, Amount)。然而,列表中的项目数量(总数组)是动态的,因此我不能进去添加视图控制器,我需要根据总项目数组的数量动态地添加或删除它们。

我试着添加一个循环,根据数组中的项目数量(项目)动态地创建所需数量的视图控制器(为每个页面)。然而,这并不奏效。

下面是我的ViewController。

import UIKit

class ViewController: UIViewController, UIPageViewControllerDataSource, UIPageViewControllerDelegate {

    var myControllers = [UIViewController]()

    override func viewDidLoad() {
        super.viewDidLoad()
        let vc = UIViewController()
        vc.view.backgroundColor = .red
        myControllers.append(vc)
    }

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)

        DispatchQueue.main.asyncAfter(deadline: .now()+2, execute: {self.presentPageVC()})
    }

    func presentPageVC() {
        guard myControllers.first != nil else {
            return
        }
        let vc = UIPageViewController(transitionStyle: .pageCurl,
                                      navigationOrientation: .horizontal,
                                      options: nil)
        vc.delegate = self
        vc.dataSource = self
        vc.setViewControllers([],
                              direction: .forward,
                              animated: true,
                              completion: nil)

        present(vc, animated:  true)
    }

    func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
        return nil
    }

    func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
        return nil
    }

}

下面是我的PageViewController:

import UIKit

class PageViewController: UIPageViewController, UIPageViewControllerDataSource, UIPageViewControllerDelegate {

var arrayOfItems = [Items]()
var counter = arrayOfItems.count

var subViewControllers:[UIViewController] = {
    return [
            for i in 1...counter {
                .instantiateViewController,
            }
    ]
}()

override func viewDidLoad() {
    super.viewDidLoad()

    self.delegate = self
    self.dataSource = self

    setViewControllers([subViewControllers[0]], direction: .forward, animated: true, completion: nil)
}


func presentationCount(for pageViewController: UIPageViewController) -> Int {
    return subViewControllers.count
}

func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
    let currentIndex: Int = subViewControllers.firstIndex(of: viewController) ?? 0
    if (currentIndex <= 0) {
        return nil
    }
    return subViewControllers[currentIndex - 1]
}

func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
    let currentIndex: Int = subViewControllers.firstIndex(of: viewController) ?? 0
        if (currentIndex >= subViewControllers.count - 1) {
        return nil
    }
    return subViewControllers[currentIndex + 1]
}    

}

xcode uiviewcontroller uipageviewcontroller xcode11 uipagecontrol
© www.soinside.com 2019 - 2024. All rights reserved.