1

Here I am trying to remove particular index data when delete button pressed from the array which saved using model class can anyone help me to remove data from the array and to not display in collection view ?

Here I had tried this code

Here I had declared like this as array

var cartItemsModel = [CartItems]()

Below is my delete button action

 @objc func deleteAction(sender : UIButton){
        let buttonPosition = sender.convert(CGPoint(), to: cartCollectionView)
        let indexPath = cartCollectionView.indexPathForItem(at: buttonPosition)
        self.deleteIndexPath = indexPath
        self.Index = sender.tag
        let obj = cartItemsModel[(indexPath?.row)!]
        self.cartItemsModel.remove(at: (indexPath?.row)!)
        self.itemId = obj.itemID
        self.price = obj.price
        //        view.isUserInteractionEnabled = false
        let attribute = RappleActivityIndicatorView.attribute(style: .apple, tintColor: .white, screenBG: .darkGray, progressBG: .black, progressBarBG: .orange, progreeBarFill: .red, thickness: 4)
        RappleActivityIndicatorView.startAnimatingWithLabel("Processing...", attributes: attribute)
        //        UIApplication.shared.beginIgnoringInteractionEvents()
        if loginCheck == 1 {
            let customerDeleteAPI = "\(domainName)/V1/carts/mine/items/\(itemId!)/"
            customerItemsDeleteDownloadJsonWithURL(customerItemsAPI: customerDeleteAPI)
        }else {
            let deleteApi = "\(domainName)/en/V1/guest-carts/\(self.guestQuoteId!)/items/\(itemId!)/"
            deleteItemsDownloadJsonWithURL(deleteItemsAPI: deleteApi)
        }
    }

Below is after calling delete api depending on response I was trying to remove the array which selected and the code is

self.cartItemsModel.remove(at: (self.deleteIndexPath?.row)!)

and I tried another code also shown below

self.cartItemsModel.remove(at: self.Index!)
User
  • 73
  • 1
  • 8
  • reload your collectionview after delete with .reloadData() – koropok Apr 04 '18 at 07:02
  • could you elaborate what you are trying to do? and about the issue, you are getting – ram880 Apr 04 '18 at 07:02
  • it crashes at this line `self.cartItemsModel.remove(at: (self.deleteIndexPath?.row)!)` @koropok – User Apr 04 '18 at 07:05
  • I was just removing the array data locally and to reload collection instead of calling cart items api @ram880 – User Apr 04 '18 at 07:05
  • Are you saying you are doing `self.cartItemsModel.remove` _again_ after calling the api? You are already removing the item in the code you showed us here. – The Dark Apr 04 '18 at 07:12
  • you can this method for deleting self.collectionView.deleteItems(at: [indexpaths]) to delete. Then remove from array like self.cartItemsModel.remove(at: indexpath.item) . and make sure api call success before you proceed to delete. – ram880 Apr 04 '18 at 07:13

1 Answers1

0

Please check the below-tagged answer. It will solve your problem.

delete item from collection view

But after your successful API call uses these methods.

Also, remove the item from the array first before collectionView.deleteItems(at: [])

ram880
  • 1,288
  • 12
  • 22