Swift:在获取segue以显示视图集合中选择项目的详细信息时遇到问题



当点击项目的图像时,我正试图将View Collection设置为分段到项目的详细信息。以下是我的代码。然而,当我尝试时,我得到了错误,线程1:致命错误:索引超出范围,在这行代码旁边:destinationController.shop=shopping[indexPaths[0].row]

这是我的集合视图控制器的完整代码。有人能告诉我我做错了什么吗?

import UIKit
private let reuseIdentifier = "Cell"
class shopCollectionViewController: UICollectionViewController {
@IBAction func unwindToMain(segue: UIStoryboardSegue){

}
var shopping: [Shop] = [Shop(image:"blueTshirt", name:"blueTshirt", price: 10), Shop(image:"blackTshirt", name:"blackTshirt", price: 10), Shop(image:"lightblueTshirt", name:"lightblueTshirt", price: 10), Shop(image:"whiteTshirt", name:"whiteTshirt", price: 10)]

override func viewDidLoad() {
super.viewDidLoad()
// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = false
// Do any additional setup after loading the view.
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
// MARK: UICollectionViewDataSource
override func numberOfSections(in collectionView: UICollectionView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}

override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of items
return shopping.count
}
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "dataCell", for: indexPath) as! shopCollectionViewCell
// Configure the cell
let item = shopping[indexPath.row]
cell.itemImage.image = UIImage(named: item.image)
return cell
}
override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
performSegue(withIdentifier: "showDetail", sender: nil)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "showDetail" {
if let indexPaths = collectionView.indexPathsForSelectedItems{
let destinationController = segue.destination as! itemDetailsViewController
destinationController.shop = shopping[indexPaths[0].row]
collectionView.deselectItem(at: indexPaths[0], animated: false)
}
}

使用collectionView时,可能应该使用indexPath.item而不是indexPath.row

indexPathsForSelectedItems更可靠的方法是在调用performSegue时传递索引路径。

并取消选择didSelectItemAt内的单元格

override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
performSegue(withIdentifier: "showDetail", sender: indexPath)
collectionView.deselectItem(at: indexPath, animated: false)
}

用代替prepare(for

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "showDetail" {
let indexPath = sender as! IndexPath
let destinationController = segue.destination as! itemDetailsViewController
destinationController.shop = shopping[indexPath.item]
}
}

相关内容

  • 没有找到相关文章

最新更新