【发布时间】:2018-02-22 07:54:10
【问题描述】:
我的目标是执行从集合视图单元到视图控制器的 segue。在下一个视图控制器中我有声明
let selectedCar = CarViewController()
if selectedCar.selectedMaker == "a"
我知道使用附加类执行 segue 的方法,但不知道如何使用集合视图执行。我将 indexPath 与 if else 语句一起使用。 Segue 名称是“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")
}
【问题讨论】:
-
didSelectItemAt 工作正常。我认为将变量 selectedMaker 传递给下一个视图控制器时遇到问题
标签: swift collections view segue