Arrays 单击collectionView转到另一个具有不同阵列的collectionView

Arrays 单击collectionView转到另一个具有不同阵列的collectionView,arrays,swift,xcode,Arrays,Swift,Xcode,我对斯威夫特相当陌生,有一个问题我想问 我有三个ViewController,前两个有集合视图,最后一个没有 我拥有它,因此第一个控制器在集合视图中显示两个数组。第一个是字符串数组,第二个是图像数组,这样它们就可以在集合视图上正确对齐 这是我正在努力理解的。当单击集合视图单元格时,如何让它转到第二个控制器(其中有另一个集合视图)并显示完全不同的数组。我希望这样,每次单击第一个控制器时,每个单元格都会显示一个不同但特定的数组 单击第二个视图控制器集合单元格后,它将转到具有图像、标签和文本视图的视图

我对斯威夫特相当陌生,有一个问题我想问

我有三个ViewController,前两个有集合视图,最后一个没有

我拥有它,因此第一个控制器在集合视图中显示两个数组。第一个是字符串数组,第二个是图像数组,这样它们就可以在集合视图上正确对齐

这是我正在努力理解的。当单击集合视图单元格时,如何让它转到第二个控制器(其中有另一个集合视图)并显示完全不同的数组。我希望这样,每次单击第一个控制器时,每个单元格都会显示一个不同但特定的数组

单击第二个视图控制器集合单元格后,它将转到具有图像、标签和文本视图的视图控制器。我需要为单击的每个单元格显示一组不同的数据

我真的不知道从哪里开始,我相信这可能与字典有关,但我不知道从哪里开始

这是我在集合视图的第一个视图控制器中使用的代码


class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {


    @IBOutlet weak var collectionView: UICollectionView!


    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        collectionView.delegate = self
        collectionView.dataSource = self

    }


    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize{
          return CGSize(width: 160.0, height: 210.0)
       }


    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return labelArray.count
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CollectionViewCell", for: indexPath) as! CollectionViewCell

        cell.imageView.image = imageArray[indexPath.item]
        cell.label.text = labelArray[indexPath.item]

        return cell
    }

}
我只是不知道从这里到哪里去,任何帮助都将不胜感激!
谢谢]

您需要实现
func-collectionView(\ucollectionview:UICollectionView,didSelectItemAt-indexPath:indexPath)
。看

在它里面,你可以调用你的segue,它会导航到下一个控制器,例如:

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    // Get selected row, you can then do something for each row
    let selectedRow = indexPath.row // can also get indexPath.section or .item
    self.performSegue(withIdentifier: "your identifier", sender: self)
}
如果要将某些数据从第一个控制器传递到下一个控制器,可以在
覆盖函数prepareforsgue(segue:UIStoryBoardSegue,sender:AnyObject?)中传递数据

当然,我们假定
YourViewControllerType
将有一个成员
var anotherArray:[String]?
(类型和名称当然可以更改)


更好的方法是定义一个适当的模型类,该类将保存目标控制器的数据。

非常感谢您的回答,我现在理解得更多了,我只是想问一下,我该如何区分单元格,这样当我单击每个单元格时,它们都会做一些不同的事情,这是我最后一件挣扎的事情。谢谢@
didSelectItemAt
中的StefanFletcher
indepath
参数提供的行ID与
cellForItemAt
方法中的行ID完全相同。所以知道行ID就可以决定做什么。我将稍微扩展示例以显示
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "your identifier" {
        var dest = segue.destinationViewController as! YourViewControllerType
        dest.anotherArray = ["whatever", "you", "want"]
}