TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

Refreshing a collection of views after deleting one view from the collection

Forums > SwiftUI

Hello, I have screen with a number of text and image views (cards in a player's hand). After tapping a view (card) 3 times with an .onTapGesture, I remove that view(card) from the array of that player's hand. Then I want to then show the remainder of the text and image views that the player now has in their hand. If I go back to my menu of different navigationlinks and select again that collection of views or cards in the player's hand, it is updated. I am trying to figure out how to 'refresh' without going back to the menu.

Thanks, Kelly

2      

@Kelly is trying thinking procedurally, instead of declaratively!

I am trying to figure out how to 'refresh' without going back to the menu.

Declare your Intentions!

Kelly! Remember that SwiftUI is a declarative language. First and foremost, just tell SwiftUI your intentions.

For example:

<< Hey! SwiftUI, I always want to show the user the cards that are in their hand. Always! >>

Now you need to provide the view with a model that represents the cards in your player's hand. If the model has six cards, SwiftUI will display the six cards. If you tap on one card, the .onTapGesture should remove ONE card from your player's hand. How? The code should not try to update the user interface. Instead, the code should just CHANGE THE MODEL.

Change the Model

That is, the model, probably an array of the player's hand will now only have five cards in it. Because the model has been updated, the user interface will invalidate itself and ask SwiftUI to update the PlayerHandView.

2      

Hacking with Swift is sponsored by RevenueCat.

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.