如何从不同的ViewController执行collectionView类

时间:2019-06-30 05:37:44

标签: ios swift uicollectionview

我有一个 collectionView

class LC: UIViewController,UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout{

//in LC

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if let VC = segue.destination as? VC2 {
        VC.Rpe = Pass
    }
}

工作正常,在 VC2 中,我有一个函数,在执行时应该将其锁定到collection view

中的下一个单元格中

我不确定执行此操作的最佳方式或方法是什么(reload VC2 ,其中包含下一个collection view cell详细信息?还是运行{{1} }以编程方式运行)

更新

collection view

数据

import Foundation
import UIKit

class View2: UIViewController {

@IBOutlet var Q_Pic: UIImageView!
@IBOutlet var Q_que: UILabel!
var SelectedCell: Ques!

override func viewDidLoad() {
    super.viewDidLoad()

    Q_Pic.image = UIImage(named: SelectedCell.LIMG)
    Q_que.text = SelectedCell.Q
}

@IBAction func herewego(_ sender: Any) {
    print("when the user press this button it should take him directly to the next cell detail , i don't want the user to go back to collection view and choose the next cell")
}
}

UICollectionViewController

let Q_A_TEST_MOH = [
Ques(Q: "Q1? ",LIMG: "1"),
Ques(Q: "Q2? ",LIMG: "2"),
Ques(Q: "Q3?",LIMG: "3"),
Ques(Q: "Q4?",LIMG: "4"),
Ques(Q: "Q5?",LIMG: "5")
]

struct Ques {
var Q : String
var LIMG: String
}

UICollectionViewCell

import Foundation
import UIKit

class test:UIViewController,UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout {

@IBOutlet var CollectionView: UICollectionView!
var Levelssss: [Ques]!
var ToPass: Ques!
var SelectedCategory: String!
var Level: Int!
override func viewDidLoad() {
    super.viewDidLoad()
    CollectionView.delegate = self
    CollectionView.dataSource = self
}

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

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "LevelCell2", for: indexPath) as? cell1 {
        let r = Q_A_TEST_MOH[indexPath.item]
        cell.congigureCell(EditLater: r)
        return cell
    }
    return UICollectionViewCell()
}

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    ToPass = Q_A_TEST_MOH[indexPath.item]
    performSegue(withIdentifier: "To", sender: self)
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if let detalsVC = segue.destination as? View2 {
        detalsVC.SelectedCell = ToPass
    }
}
}

从此处下载项目:download the project

1 个答案:

答案 0 :(得分:0)

这是固定的变体:https://www.dropbox.com/s/bc7ktktrbqg9x7t/test%202.zip?dl=0

逻辑很简单:传递整个数据数组和所选对象的索引。

VC2按钮上单击,您只需增加索引并更新视图的内容即可。

现在您应该检查索引是否大于数组中元素的数量。

相关问题