Moving the rows in a List view
In this recipe, we’ll create an app that implements a List view that allows users to move and reorganize rows.
Getting ready
Create a new SwiftUI project named MovingListRows.
How to do it…
To make the List view rows movable, we’ll add a modifier to the List view’s ForEach struct, and then we’ll embed the List view in a navigation view that displays a title and an edit button. The steps are as follows:
- Add a @Statevariable to theContentViewstruct that holds an array of countries:@State private var countries = ["USA", "Canada", "Mexico", "England", "Spain", "Cameroon", "South Africa" , "Japan", "South Korea"]
- Replace the body variable’s text view with a NavigationStack, aList, and modifiers for navigating. Also, notice that the.onMovemodifier is applied to theForEach...
 
                                             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
     
         
                 
                 
                 
                 
                 
                 
                 
                 
                