【问题标题】:Click on collectionView go to another collectionView with different array单击 collectionView 转到另一个具有不同数组的 collectionView
【发布时间】:2019-10-11 15:11:13
【问题描述】:

我对 swift 还很陌生,有一个问题想问。

我有三个 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
    }

}

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

【问题讨论】:

    标签: arrays swift xcode


    【解决方案1】:

    你需要实现func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath)。见Apple docs

    在里面,你可以调用你的 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)
    }
    

    如果您想将一些数据从第一个控制器传递到下一个控制器,您可以在override func prepareForSegue(segue: UIStoryBoardSegue, sender: AnyObject?) 中传递一个数据:

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if segue.identifier == "your identifier" {
            var dest = segue.destinationViewController as! YourViewControllerType
            dest.anotherArray = ["whatever", "you", "want"]
    }
    

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

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

    【讨论】:

    • 非常感谢您的回答,我现在明白了一点,我只是想问一下,我将如何区分单元格,所以当我单击每个单元格时,它们都会做不同的东西,这只是我挣扎的最后一件事。谢谢!
    • @StefanFletcher indexPath didSelectItemAt 中的参数为您提供与 cellForItemAt 方法中完全相同的行 ID。所以知道行 ID 你可以决定做什么。我将稍微扩展示例以显示
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-14
    • 1970-01-01
    • 2016-07-26
    相关资源
    最近更新 更多