SwiftUI SwiftUI Lists and Forms 1 — Questions and Answers
Question 1: Which SwiftUI view renders a scrollable list of rows?
- ScrollView
- LazyVStack
- List (Correct answer)
- Table
Correct answer: List
`List` renders a platform-styled scrollable list with built-in support for separators, selection, and swipe actions.
Question 2: How do you make List rows deletable in SwiftUI?
- .onDelete(perform:) inside ForEach (Correct answer)
- .swipeToDelete()
- List(onDelete:)
- .deleteEnabled(true)
Correct answer: .onDelete(perform:) inside ForEach
Adding `.onDelete(perform:)` to a `ForEach` inside a `List` enables swipe-to-delete with the system delete gesture.
Question 3: What modifier enables drag-to-reorder rows in a SwiftUI List?
- .onReorder()
- .onMove(perform:) (Correct answer)
- .draggable()
- .moveDisabled(false)
Correct answer: .onMove(perform:)
`.onMove(perform:)` added to a `ForEach` inside a `List` enables the drag reorder handle when the list is in edit mode.
Question 4: Which property wrapper provides edit mode state to a SwiftUI List?
- @State
- @Environment(\.editMode) (Correct answer)
- @Binding
- @ObservedObject
Correct answer: @Environment(\.editMode)
`@Environment(\.editMode)` reads and writes the current edit mode, enabling selection and reorder controls in a List.
Question 5: How do you add a section with a header to a SwiftUI List?
- List(header:)
- Section(header:) { rows } (Correct answer)
- .listHeader()
- Group(header:)
Correct answer: Section(header:) { rows }
Wrap rows in a `Section` view with a `header` parameter to create grouped sections inside a List.
Question 6: Which List style makes rows appear as separated inset cards?
- .listStyle(.plain)
- .listStyle(.insetGrouped) (Correct answer)
- .listStyle(.sidebar)
- .listStyle(.grouped)
Correct answer: .listStyle(.insetGrouped)
`.listStyle(.insetGrouped)` applies the inset grouped style, rendering sections as rounded-corner card groups.
Which SwiftUI view renders a scrollable list of rows?