How to fix Thread 1: Fatal error: Index out of range

How to fix Thread 1: Fatal error: Index out of range

我的问题是当我点击集合视图时它显示数据但是如果我点击其他集合视图它显示错误:

Thread 1: Fatal error: Index out of range. 

如何解决?谢谢

这是我的代码

import UIKit

struct item {

    var name : String
    var price : String
    var image : String
}

class SearchViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {


    var items = [item]()


    @IBOutlet weak var collectionView: UICollectionView!

    @IBOutlet weak var collectionviewflow: UICollectionViewFlowLayout!

    override func viewDidLoad() {
        super.viewDidLoad()
        collectionView.delegate = self
        collectionView.dataSource = self

    }

    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return items.count
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! CollectionViewCell
        let cellIndex = indexPath.item
        cell.imageView.image = UIImage(named: items[indexPath.item].image)
        cell.labelname.text = items[indexPath.item].name
        cell.labelprice.text = items[indexPath.item].price

        cell.contentView.layer.cornerRadius = 10
        cell.contentView.layer.borderWidth = 1.0
        cell.contentView.layer.borderColor = UIColor.gray.cgColor
        cell.backgroundColor = UIColor.white

        cell.layer.shadowColor = UIColor.gray.cgColor
        cell.layer.shadowOffset = CGSize(width: 0.0, height: 2.0)
        cell.layer.shadowRadius = 2.0
        cell.layer.shadowOpacity = 1.0
        cell.layer.masksToBounds = false
        cell.layer.shadowPath = UIBezierPath(roundedRect: cell.bounds, cornerRadius: cell.contentView.layer.cornerRadius).cgPath

        return cell
    }

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let vc = storyboard!.instantiateViewController(identifier: "ItemDetailViewController") as! ItemDetailViewController

        vc.name = items[indexPath.item].name
        vc.price = items[indexPath.item].price
        vc.imagee = items[indexPath.item].image

        self.navigationController?.pushViewController(vc, animated: true)    }

    override func viewWillDisappear(_ animated: Bool) {
        items.removeAll()
    }

}

extension SearchViewController : UICollectionViewDelegateFlowLayout {

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
        return UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
    }


    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
        return 0
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return 10
    }
}

这是我的 itemdetailsviewcontroller

import UIKit


class ItemDetailViewController: UIViewController {

    var name : String = ""
    var price : String = ""
    var imagee : String = ""

    @IBOutlet weak var labelname: UILabel!

    @IBOutlet weak var image: UIImageView!

    @IBOutlet weak var labelprice: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()

        labelname.text = name
        labelprice.text = price
        image.image = UIImage(named: imagee)

    }

}

viewWillDisappear(_ animated: Bool) 中删除 items.removeAll()。您正在更改 collectionView 的源数据,而它并不知道。当您第二次点击一个单元格时,collectionView 正在尝试使用 items 中的一个项目,但是当您在第一个单元格选择上按下控制器时,您清空了它。