在Swift中为PageViewController创建一个UICollectionViews数组

时间:2017-10-04 10:36:39

标签: ios arrays swift uicollectionview uipageviewcontroller

我已经设置了一个CollectionViewController,它将从数据源创建一个CollectionView。现在我想使用PageViewController通过CollectionViews进行滑动,但我不知道如何创建包含CollectionViews的数组。

到目前为止,我只通过以下方式呈现了一个CollectionView:

let CollectionVC = storyboard?.instantiateViewController(withIdentifier: "CollectionView") as! CollectionViewController
    CollectionVC.itemStrings = matrixArray[0].makeString()
    CollectionVC.columns = problem.columns
    CollectionVC.rows = problem.rows
    self.present(CollectionVC, animated: true, completion: nil)

这样可以很好地呈现一个视图。

但是现在我需要创建一个可以由PageViewController读取以生成PageView的数组,我该怎么做?我甚至不确定它对于PageViewController数据源需要的格式,据我可以从其他教程看到,它应该是UIViewController的数组而不是我计划做的类型UICollectionViewController,如果是这样的话,如何使用CollectionViews进行工作?

有关如何创建可用作PageViewController数据源的数组的任何建议吗?

有关更多说明

这是一张如何看待结尾的图片:

CollectionView

每个屏幕基本上都是由CollectionViewController创建的,而PageViewController正在组织它们,并且可以按照分页的方式通过这些屏幕进行滑动。

//编辑:

感谢Puneet Sharma的回答。我确定这已经非常接近我正在寻找的解决方案。但由于我在编程方面是一个完全自学成才的菜鸟,我不完全确定在哪里适合所有这些信息。

到目前为止,我有2个控制器,一个ViewController:

class ViewController: UIViewController {

//Initialize Variables:
var matrixArray = Array<Matrix>()
var basicArray = Array<Array<Int>>()
var maxArray = Array<Double>()
var currentSolutionArray = Array<Array<Double>>()
var isOptimal = false
var isCyceling = false
var CollectionViewArray: [UICollectionView] = []

//The outlets are for later
@IBOutlet weak var myObjectionFunction: UITextField!

@IBOutlet weak var myConstraint1: UITextField!
@IBOutlet weak var myConstraint1RightSide: UITextField!

@IBOutlet weak var myConstraint2: UITextField!
@IBOutlet weak var myConstraint2RightSide: UITextField!

@IBOutlet weak var myConstraint3: UITextField!
@IBOutlet weak var myConstraint3RightSide: UITextField!


//StartButton
@IBAction func startButton(_ sender: Any) {

    //Initializing Objection Function
    let objFunction = [-40.0,-30.0,0.0,0.0,0.0,0.0]

    // Initializing Nr. of Rows and Columns of the Matrix
    let rows = 4
    let columns = 6

    // Initializing constraints
    let constraint0 = [1.0,1.0,1.0,0.0,0.0,8.0]
    let constraint1 = [2.0,1.0,0.0,1.0,0.0,12.0]
    let constraint2 = [2.0,3.0,0.0,0.0,1.0,18.0]

    //Initalize an Array containing all constraints
    let constraints = [constraint0, constraint1, constraint2]

    //Alternative for use of UserData -> Delete objFuntion, rows, columns, constraint0..2, constraintsArray
    //let problem = getData().0
    //let constraints = getData.1
    let problem = LinearProblem.createLP(rows: rows, columns: columns, objFunction: objFunction, constraints: constraints)


    // Initializing Current solution (No need to change anything)
    let currentSolution = Array(repeating: 0.0, count: constraints.count)



    print("Initial Problem: ")
    print(problem)
    print("\n")


   let solution = PrimalSimplex(problem: problem, currentSolution: currentSolution)

    matrixArray = solution.0
    basicArray = solution.1
    maxArray = solution.2
    currentSolutionArray = solution.3
    isOptimal = solution.4
    isCyceling = solution.5

    let CollectionVC = storyboard?.instantiateViewController(withIdentifier: "CollectionView") as! CollectionViewController
    CollectionVC.itemStrings = matrixArray[0].makeString()
    CollectionVC.columns = problem.columns
    CollectionVC.rows = problem.rows
    self.present(CollectionVC, animated: true, completion: nil) 
 }
}

它基本上是一个带有开始按钮的屏幕,它在推送时启动算法并显示下一个视图。

然后这个CollectionViewController:

class CollectionViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {

@IBOutlet weak var myCollectionView: UICollectionView!

    //Is also set in Storyboard
    let reuseIdentifier = "cell" 

    var itemStrings: Array<String?> = []
    var rows: Int = 0
    var columns: Int = 0   


//
//Setup CollectionView: Table to display LPs including Datasource
//

// MARK: - UICollectionViewDelegate protocol

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    // handle tap events
    let newPath = indexPath.item + 1
    print("You selected cell #\(newPath)")
}

// calculate height and width of cell
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {

    let width = collectionView.bounds.width
    let gaps = columns-1

    let tmpWidth = width - CGFloat(gaps)
    let itemWidth = tmpWidth / CGFloat(columns)

    let height = collectionView.bounds.height

    let tmpHeight = height - CGFloat(gaps)
    let itemHeight = tmpHeight / CGFloat(rows)

    return CGSize(width: itemWidth, height: itemHeight)
}

// CellSpacing vertical
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {

    return CGFloat(3)
}

// CellSpacing horizontal
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {

    return CGFloat(1)
}

// change background color when user touches cell
func collectionView(_ collectionView: UICollectionView, didHighlightItemAt indexPath: IndexPath) {
    let cell = collectionView.cellForItem(at: indexPath)

    //Change color
    if cell?.backgroundColor == UIColor(red:0.96, green:0.40, blue:0.40, alpha:1.0) {
        cell?.backgroundColor = UIColor(red:0.94, green:0.94, blue:0.94, alpha:1.0)
    }
    else{
        cell?.backgroundColor = UIColor(red:0.96, green:0.40, blue:0.40, alpha:1.0)
    }
}

// MARK: - UICollectionViewDataSource protocol

// tell the collection view how many cells to make
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    let numberOfItems = rows*columns
    return numberOfItems
}

// make a cell for each cell index path
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    // get a reference to our storyboard cell
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath as IndexPath) as! MyCollectionViewCell

    // Use the outlet in our custom class to get a reference to the UILabel in the cel

    cell.myLabel.text = itemStrings[indexPath.item]
    cell.backgroundColor = UIColor(red:0.94, green:0.94, blue:0.94, alpha:1.0)

    // Change shape of cells
    cell.layer.cornerRadius = 8

    return cell
 }
}

这些目前也是我的故事板中的唯一元素。

我想我需要在Storyboard中添加PageViewController并用HomeViewController的代码表填充它。然后将故事板中的segue从开始按钮更改为HomeViewController。

我不理解的部分是将要创建的ChildVCs。我是否需要将该部分添加到我的ViewController中?那么IndexedCollectionViewController类,我需要为此创建一个额外的文件,还是可以将其放入我的collectionView中?

这个功能怎么样?

private func viewController(atIndex index:Int) -> IndexedCollectionViewController

去哪儿了?我认为在我的情况下,这将在ViewController类中,Id只是在开始按钮中调用方法?

对不起所有这些非常简单的问题,但作为一个自学编码器,它有时很难掌握事物背后的概念^^

1 个答案:

答案 0 :(得分:0)

在我看来,UIPageViewController文档有点模糊。 争论的焦点是存在一种方法:

<?php
    // include custom jQuery
    function shapeSpace_include_custom_jquery() {
        wp_deregister_script('jquery');
        wp_enqueue_script('jquery', 'https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js', array(), null, true);
    }
    add_action('wp_enqueue_scripts', 'shapeSpace_include_custom_jquery');

    function bootstrap(){
        wp_enqueue_script( 'bootstrap_js', get_template_directory_uri() . '/bootstrap/js/bootstrap.min.js', array('jquery'), '3.3.7', true);
        wp_enqueue_style( 'bootstrap_css', get_template_directory_uri() . '/bootstrap/css/bootstrap.min.css', '3.3.7', true);
    }
    add_action('wp_enqueue_scripts', 'bootstrap');

    function theme_name_scripts() {
        wp_enqueue_style( 'default_stylesheet', get_stylesheet_uri()); 
    }
    add_action('wp_enqueue_scripts', 'theme_name_scripts');
?>

所有教程都在这个数组中传递一个视图控制器,它们是正确的。但是由于这种方法的存在,我假设我可以将所有需要显示的视图控制器提供给一个数组中的pageviewcontroller。

但是,实际上需要此方法来传递需要在动画之后显示的视图控制器。如果我们需要在屏幕上显示多个视图(视图控制器),我们可以在数组中传递这些视图。但这种情况很少发生,因此所有教程都显示在数组中传递了一个VC。

UIPageViewControllerDatasource用于提供其他视图控制器。

您可以实现以某种方式保存pageviewcontroller的viewcontroller。

func setViewControllers(_ viewControllers: [UIViewController]?, 
              direction: UIPageViewControllerNavigationDirection, 
               animated: Bool, 
             completion: ((Bool) -> Void)? = nil)

在我看来,你的视图布局应该是这样的。
VC1 : - &gt;查看具有启动按钮的控制器,并将显示vc2 VC2 : - &gt;查看控制器,显示网页浏览 它既可以是PageViewController本身(因为UIPageViewController是UIViewController的子类),也可以是一个简单的ViewController,其中PageViewController被添加为子VC。

PageViewController,实质上以页面的形式显示其他ViewControllers内容。在你的情况下,这些其他的viecontrollers是CollectionViewController。