Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/google-app-engine/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 致命错误:索引超出firebase多阵列的范围_Swift - Fatal编程技术网

Swift 致命错误:索引超出firebase多阵列的范围

Swift 致命错误:索引超出firebase多阵列的范围,swift,Swift,我制作了一个包含多个CollectionView的应用程序,CollectionView从firebase接收数据(图像),一切正常,图像出现在每个部分。直到我选择(显示详细信息)任何集合末尾的图像,但一个集合的图像数最少 收藏1有10张图片==>我可以从1张图片中选择图片 至7 收藏2有13张图片==>我可以从1张图片中选择图片 至7 收藏3有7张图片==>我可以选择所有图片 如果我选择任何高于7号的图像,应用程序就会崩溃,并出现错误“致命错误:索引超出范围” 我的代码: func c

我制作了一个包含多个CollectionView的应用程序,CollectionView从firebase接收数据(图像),一切正常,图像出现在每个部分。直到我选择(显示详细信息)任何集合末尾的图像,但一个集合的图像数最少

  • 收藏1有10张图片==>我可以从1张图片中选择图片 至7
  • 收藏2有13张图片==>我可以从1张图片中选择图片 至7
  • 收藏3有7张图片==>我可以选择所有图片
  • 如果我选择任何高于7号的图像,应用程序就会崩溃,并出现错误“致命错误:索引超出范围”

    我的代码:

        func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
           
           if collectionView == collectionView1 {
            
            return foodCollection1.count
            }
            if collectionView == collectionView2 {
            return foodCollection2.count
            }
            if collectionView == collectionView3 {
            return foodCollection3.count
            }
          
         else {
          return headerCollection.count
        }
       
    }
    
    我在let food4=foodCollection4[indexPath.row]处遇到了错误

    
      func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
            let food1  =  foodCollection1[indexPath.row]
            let food2  =  foodCollection2[indexPath.row]
            let food3  =  foodCollection3[indexPath.row]
            let food4  =  foodCollection4[indexPath.row]
            
            if collectionView == collectionView1 {
            let detailVC = storyboard?.instantiateViewController(identifier: "DetailsViewController") { (coder) in
                return DetailsViewController(coder: coder, item3: food1 )
            }
                navigationController?.pushViewController(detailVC!, animated: true)
            }
                if collectionView == collectionView2 {
    
            let detailVC2 = storyboard?.instantiateViewController(identifier: "DetailsViewController") { (coder) in
                return DetailsViewController(coder: coder, item3: food2 )
            }
                navigationController?.pushViewController(detailVC2!, animated: true)
    
            }
                    if collectionView == collectionView3 {
    
            let detailVC3 = storyboard?.instantiateViewController(identifier: "DetailsViewController") { (coder) in
                return DetailsViewController(coder: coder, item3: food3 )
            }
                navigationController?.pushViewController(detailVC3!, animated: true)
           
        
    }
    }
    

    谢谢你的帮助

    帮助我解决了这个问题。我所要做的就是:

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
            
            if collectionView == collectionView1 {
    
            let food1  =  foodCollection1[indexPath.row]
    
            let detailVC = storyboard?.instantiateViewController(identifier: "DetailsViewController") { (coder) in
                return DetailsViewController(coder: coder, item3: food1 )
            }
                navigationController?.pushViewController(detailVC!, animated: true)
            }
    
    ..... etc
    

    不要在
    didSelectIteamAt
    中执行
    let food1
    let food2
    等操作。假设第一个数组只有1项,第二个数组有7项。如果indexPath.row是6,会发生什么?你会得到那个错误。所以,不要这样做,而是根据它是哪个collectionView(如果collectionView=,则获取
    food1
    ,或
    food2
    ,等等。您需要完全重做
    didSelectItemAt
    方法。如果其中任何一个数组中有不同数量的元素,整个过程都会停止。您需要根据需要提取元素。@Frankenstein我是初学者,这是一个例子我知道的唯一方法。其他方法是什么?@Larme谢谢你,请你把答案中的最后一行整理一下。创建
    让food2=foodCollection2[indexPath.row]
    只在需要的时候。在这里,如果collectionView==collectionView2{,它就在你写的case/scope中,否则它就没用了(也没有意义)。