Krutarth Patel
Krutarth Patel

Reputation: 3455

How to delete all array if i select one of its item in swift

I have two collection views.

1)Main

2)Sub

at DidSelect Method main and sub Items is added to UITableView

Main Product is related to sub items.

Suppose in main collection view I have iPhone when I will click on iPhone it will added to cart and sub collection view will open

Code to Add product in tableview

  arrProductCart.insertObject(dictSub, atIndex: 0)
  arrMainProduct.addObject(dictSub)

Sub collection view contain products related to main items.

Suppose i choose iPhone than subitems will be charger and headphone.

My requirement is when i delete main product its related sub items should be deleted from tableview

How i am deleting individual product

    func btnDeleteTapped(sender: UIButton){
    arrProductCart.removeObjectAtIndex(sender.tag)
    tblCart.reloadData()
}

How can I achieve this?

Upvotes: 0

Views: 192

Answers (1)

Fogmeister
Fogmeister

Reputation: 77631

I'd suggest not doing that from a UX point of view.

Think of the situation where a user adds an iPhone and then headphones and a charger as subitems.

Now they add an iPad but no accessories (because they have them already in the cart).

Then they decide they no longer want the iPhone.

What should now happen to the iPhone subitems?

If you DO want to do this then you could store the cart almost recursively. Have a cart that includes an iPhone. But the iPhone also has an additional cart that includes the accessories.

The whole thing can be displayed as a single cart but now when you delete the iPhone the accessories will delete also.

Upvotes: 1

Related Questions