从集合视图执行 segue
perform segue from collection view
我的目标是执行从集合视图单元格到视图控制器的转换。在下一个视图控制器中,我有语句
let selectedCar = CarViewController()
if selectedCar.selectedMaker == "a"
我知道如何使用额外的 class 执行 segue,但不知道如何使用集合视图执行。我将 indexPath 与 if else 语句一起使用。
转场名称是 "toModels"
class CarsViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {
public var selectedMaker = ""
@IBOutlet weak var collectionView: UICollectionView!
let mainMenu = ["a","b"]
override func viewDidLoad() {
super.viewDidLoad()
collectionView.delegate = self
collectionView.dataSource = self
}
public func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return mainMenu.count
}
public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "customCell", for: indexPath) as! CustomCollectionViewCell
cell.imageCell.image = UIImage(named: mainMenu[indexPath.row])
cell.labelCell.text = mainMenu[indexPath.row].capitalized
return cell
}
public func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
performSegue(withIdentifier: "toModels", sender: selectedMaker)
if indexPath == [0, 0]
{
selectedMaker = "a"
print("a")
}
else if indexPath == [0, 1]
{
selectedMaker = "b"
print("b")
}
试试这个编辑
public func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
if indexPath.item == 0
{
selectedMaker = "a"
print("a")
}
else if indexPath.item == 1
{
selectedMaker = "b"
print("b")
}
performSegue(withIdentifier: "toModels", sender: selectedMaker)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?)
{
if (segue.identifier == "toModels")
{
let objVC = segue.destination as! YOUR_NEXT_VIEWCONTROLLER
objVC.strMaker = sender as? String ?? ""
}
}
Define in YOUR_NEXT_VIEWCONTROLLER
public var strMaker = ""
我的目标是执行从集合视图单元格到视图控制器的转换。在下一个视图控制器中,我有语句
let selectedCar = CarViewController()
if selectedCar.selectedMaker == "a"
我知道如何使用额外的 class 执行 segue,但不知道如何使用集合视图执行。我将 indexPath 与 if else 语句一起使用。 转场名称是 "toModels"
class CarsViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {
public var selectedMaker = ""
@IBOutlet weak var collectionView: UICollectionView!
let mainMenu = ["a","b"]
override func viewDidLoad() {
super.viewDidLoad()
collectionView.delegate = self
collectionView.dataSource = self
}
public func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return mainMenu.count
}
public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "customCell", for: indexPath) as! CustomCollectionViewCell
cell.imageCell.image = UIImage(named: mainMenu[indexPath.row])
cell.labelCell.text = mainMenu[indexPath.row].capitalized
return cell
}
public func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
performSegue(withIdentifier: "toModels", sender: selectedMaker)
if indexPath == [0, 0]
{
selectedMaker = "a"
print("a")
}
else if indexPath == [0, 1]
{
selectedMaker = "b"
print("b")
}
试试这个编辑
public func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
if indexPath.item == 0
{
selectedMaker = "a"
print("a")
}
else if indexPath.item == 1
{
selectedMaker = "b"
print("b")
}
performSegue(withIdentifier: "toModels", sender: selectedMaker)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?)
{
if (segue.identifier == "toModels")
{
let objVC = segue.destination as! YOUR_NEXT_VIEWCONTROLLER
objVC.strMaker = sender as? String ?? ""
}
}
Define in YOUR_NEXT_VIEWCONTROLLER
public var strMaker = ""