如何从不同的ViewController执行collectionView类

一个人

我有一个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 VC2collection view cell?或以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")
}
}

数据

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
}

UICollectionViewController

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
    }
}
}

UICollectionViewCell

import UIKit

class cell1: UICollectionViewCell {

@IBOutlet var BB: UIButton!

override func awakeFromNib() {
    super.awakeFromNib()
    BB.layer.cornerRadius = 10
}

func congigureCell(EditLater: Ques){
    BB.setImage(UIImage(named: EditLater.LIMG), for: .normal)
}
}

从此处下载项目:下载项目

安德鲁

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

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

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

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章