简体   繁体   中英

how to execute collectionView class from different ViewController

i have a collectionView class

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

it's working fine , in VC2 i have a function when executed it should segue to the next cell in the collection view

i'm not sure how or what is the best way to do it ( reload VC2 with the next collection view cell details?, or run the collection view functions programmatically )

update

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

data

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

Download project from here : download the project

Here is the fixed variant: https://www.dropbox.com/s/bc7ktktrbqg9x7t/test%202.zip?dl=0

Logic is simple: pass a whole array of data and index of selected object.

In VC2 on button click you just increment index and update content of your views.

Now you should just check is index not greater then count of element in array.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM