如何修复线程1:致命错误:索引超出范围

时间:2020-05-17 17:32:38

标签: ios swift xcode collectionview

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

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)

    }

}

1 个答案:

答案 0 :(得分:0)

items.removeAll()中删除viewWillDisappear(_ animated: Bool)。您正在更改collectionView的源数据,而无需注意。当您第二次点击一个单元格时,collectionView试图使用items中的一个项目,但是在将控制器按在第一个单元格选择上时将其清空。