ava
ava

Reputation: 1176

Add Item in UICollectionView From Another Another CollectionView (Swift)

i have two collection view in one view controller. Top collection view is empty and i want that when i select any item in lower collection view this item add in top collection view.the problem is when i select item in lower collection view application crashes and i get this error :

reason: 'Invalid update: invalid number of items in section 0. The number of items contained in an existing section after the update (1) must be equal to the number of items contained in that section before the update (0), plus or minus the number of items inserted or deleted from that section (0 inserted, 0 deleted) and plus or minus the number of items moved into or out of that section (0 moved in, 0 moved out).'

there is my code, how can solve this problem?

  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)
    }

Upvotes: 0

Views: 1842

Answers (1)

dvp.petrov
dvp.petrov

Reputation: 1120

The problem is in this method:

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)
            } 
    }

It should be something like:

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)
        } 
}

Upvotes: 1

Related Questions