Adding rows to a list
The most common actions users might want to be able to perform on a list include adding, editing, and deleting items.
In this recipe, we’ll go over the process of implementing those actions on a SwiftUI list.
Getting ready
Create a new SwiftUI project and call it ListRowAdd.
How to do it…
Let’s create a list with a button at the top that can be used to add new rows to the list. The steps are as follows:
- Create a state variable in the ContentViewstruct that holds an array of numbers:@State var numbers = [1,2,3,4]
- Replace the content of the body property of the ContentViewstruct with aNavigationStackcontaining aListview:NavigationStack { List{ ForEach(self.numbers, id:\.self){ number in Text("\(number)") } } }
- Add a .navigationTitleand anavigationBarTitleDisplayModemodifier to the list with a title and an...
 
                                             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
     
         
                 
                 
                 
                 
                 
                 
                 
                 
                