Reputation: 89
I am trying to delete a cell form UICollectionView but I am having an error for "Invalid Updates"
'Invalid update: invalid number of items in section 0. The number of items contained in an existing section after the update (9) must be equal to the number of items contained in that section before the update (9), plus or minus the number of items inserted or deleted from that section (0 inserted, 1 deleted) and plus or minus the number of items moved into or out of that section (0 moved in, 0 moved out).'
and I tried updating my DataModel before removing the items from the CollectionView, but it didn't work.
here is my Code:
func didChangeQunatityOfCartProductAt(index: IndexPath?, product: ItemsModel?) {
if let quantity = product?.quantity{
if let indexPath = index{
if quantity == 0{
self.products.remove(at: indexPath.row)
self.collectionView.deleteItems(at: [indexPath])
}
}
}
}
and here is my numberOfItemsInSection Function:
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return products.count
}
I keep getting the same results even though all I have found on the web is same solution.
Upvotes: 0
Views: 4573
Reputation: 163
If your quatity of products is 0 how do you remove a product ? You should remove self.products.remove from that code because that product don't exist anymore.
func didChangeQunatityOfCartProductAt(index: IndexPath?, product: ItemsModel?) {
if let quantity = product?.quantity{
if let indexPath = index{
if quantity == 0{
self.collectionView.deleteItems(at: [indexPath])
}
}
}
}
Upvotes: 0
Reputation: 89
I have found the issue, I was reloading the collection view automatically after removing the item at index, where the products array has a property observer at the top of the class which reloads the collection view, and after removing it that fixed the problem
Upvotes: 1
Reputation: 446
You should try collectionview's performBatchUpdates(_:completion:)
func didChangeQunatityOfCartProductAt(index: IndexPath?, product: ItemsModel?) {
if let quantity = product?.quantity {
if let indexPath = index {
if quantity == 0 {
self.products.remove(at: indexPath.row)
self.collectionView.performBatchUpdates({
self.collectionView.deleteItems(at: [indexPath])
}){
// optional closure
}
}
}
}
}
Upvotes: 1
Reputation: 714
You should remove the indexPath.item from array(Products) as it is collectionview insted of indexPath.row.
func didChangeQunatityOfCartProductAt(index: IndexPath?, product: ItemsModel?) {
if let quantity = product?.quantity{
if let indexPath = index {
if quantity == 0 {
DispatchQueue.global(qos: .userInteractive).async { [weak self] in
self.products.remove(at: indexPath.item)
DispatchQueue.main.async { [weak self] in
self?.collectionView.deleteItems(at: [indexPath])
self?.collectionView.reloadData() // or you can reload the specific indexPath
}
}
}
}
}
Upvotes: 1
Reputation: 11
You can try removing the data from your dataSource array and calling the reloadData method to "refresh" your collectionViewDatasource
func didChangeQunatityOfCartProductAt(index: IndexPath?, product: ItemsModel?) {
if let quantity = product?.quantity {
if let indexPath = index {
if quantity == 0 {
self.products.remove(at: indexPath.row)
self.collectionView.reloadData()
}
}
}
}
The only difference here is replacing the cell removal for:
self.collectionView.reloadData()
Upvotes: 0