2017-01-06 6 views
0

私は1つのビューコントローラに2つのコレクションビューを持っています。トップコレクションビューが空で、コレクションコレクションビューでアイテムを選択するとトップコレクションビューに問題があります。コレクションビューのアプリケーションをクラッシュさせ、このエラーが発生すると問題が発生します:UICollectionViewに別のアイテムを追加するCollectionView(Swift)

理由: '無効な更新:セクション0のアイテムの数が無効です。更新(1)後に既存のセクションに含まれるアイテムの数は、更新前のセクション(0)に含まれるアイテムの数と同じでなければなりません(挿入された0個、削除された0個)と、そのセクションの内外へ移動されたアイテムの数(0個は移動し、0個は移動しなかったアイテム)をプラスまたはマイナスします。

私のコードは、どのようにこの問題を解決することができますか?

override func viewDidLayoutSubviews() { 

     super.viewDidLayoutSubviews() 

     let flowLayout = collectionView.collectionViewLayout as! UICollectionViewFlowLayout 
     self.collectionView.collectionViewLayout = flowLayout 

     flowLayout.estimatedItemSize = CGSize(width: collectionView.bounds.width/3, height:32) 
     flowLayout.minimumLineSpacing = 2 
     flowLayout.minimumInteritemSpacing = 2 

     let flowLayout1 = self.topCollectionView.collectionViewLayout as! UICollectionViewFlowLayout 
     self.topCollectionView.collectionViewLayout = flowLayout1 

     flowLayout1.estimatedItemSize = CGSize(width: collectionView.bounds.width/3, height:32) 
     flowLayout1.minimumLineSpacing = 2 
     flowLayout1.minimumInteritemSpacing = 2 

     self.collectionView.collectionViewLayout.invalidateLayout() 
     self.topCollectionView.collectionViewLayout.invalidateLayout() 

    } 

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 


     if collectionView == self.collectionView { 

      return foodData.count 
     } 

     else { 
     return selectFood.count 

     } 

    } 

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

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 

     let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "LCell", for: indexPath)as! CollectionLabelCell 

     if collectionView == self.collectionView { 

      cell.titleLabel.text = foodData[indexPath.row] 

     } 
     else if collectionView == self.topCollectionView { 

      if selectFood.count != 0 { 

      cell.titleLabel.text = selectFood[indexPath.row] 

      } 
     } 

     return cell 

    } 


    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { 

     if collectionView == self.collectionView { 
     self.selectFood.append(foodData[indexPath.row]) 

      self.topCollectionView.performBatchUpdates({() -> Void in 

       self.topCollectionView.reloadData() 


       }, completion:nil) 
      } 
    } 

    func collectionView(_ collectionView: UICollectionView, 
         layout collectionViewLayout: UICollectionViewLayout, 
         sizeForItemAt indexPath: IndexPath) -> CGSize { 

      return CGSize(width: collectionView.bounds.width/3, height:32) 
    } 

答えて

1

問題は、この方法である:

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { 

     if collectionView == self.collectionView { 
     self.selectFood.append(foodData[indexPath.row]) 

      self.topCollectionView.performBatchUpdates({() -> Void in 

       self.topCollectionView.reloadData() 


       }, completion:nil) 
      } 
    } 

それはのようなものでなければなりません:

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { 

    if collectionView == self.collectionView { 
    self.selectFood.append(foodData[indexPath.row]) 

     self.topCollectionView.performBatchUpdates({ [weak self]() -> Void in 

      self?.topCollectionView.insertItems(at: <#T##[IndexPath]#>) 


      }, completion:nil) 
     } 
} 
+0

のが動作することを返信用のおかげも – ava

+0

のthatsはうまく動作しますが、時に選択し、最後の項目の下でIndexPathにアイテムを追加できないため、コレクションビューのアプリケーションがクラッシュする – ava

関連する問題