import SwiftUI
struct DynamicListScreen: View {
@State private var items = ["Item 1", "Item 2", "Item 3"]
var body: some View {
VStack {
Button(action: {
let newItem = "Item \(items.count + 1)"
items.append(newItem)
}) {
Text("+ Add Item")
.font(.headline)
.padding()
}
List {
ForEach(items, id: \.self) { item in
Text(item)
}
}
}
.navigationTitle("Dynamic List Screen")
}
}
struct DynamicListScreen_Previews: PreviewProvider {
static var previews: some View {
NavigationView {
DynamicListScreen()
}
}
}This screen uses a SwiftUI List to show items from a dynamic array called items. The @State property wrapper lets the view update when the array changes.
The '+ Add Item' button adds a new string to the array. Because the array is marked with @State, SwiftUI automatically refreshes the list to show the new item. This shows why lists are great for dynamic content: they update automatically when the data changes.