Lists show changing information that can grow or shrink. They help apps display many items without fixed size.
0
0
Why lists present dynamic content in iOS Swift
Introduction
Showing messages in a chat app that update as new messages arrive.
Displaying a list of contacts that can change when you add or remove people.
Presenting search results that update as you type.
Showing a feed of posts that loads more when you scroll down.
Listing tasks that you can add, complete, or delete.
Syntax
iOS Swift
struct ContentView: View {
let items: [String]
var body: some View {
List(items, id: \.self) { item in
Text(item)
}
}
}The List view shows rows for each item in a collection.
The id: \.self tells SwiftUI how to identify each item uniquely.
Examples
Shows a list of three fruits.
iOS Swift
List(["Apple", "Banana", "Cherry"], id: \.self) { fruit in Text(fruit) }
Shows an empty list with no rows.
iOS Swift
List([] as [String], id: \.self) { item in Text(item) }
Shows a list with a single row.
iOS Swift
List(["Only one item"], id: \.self) { item in Text(item) }
Sample App
This app shows a list of fruits. When you tap the button, it adds "Orange" to the list. The list updates automatically to show the new fruit.
iOS Swift
import SwiftUI struct ContentView: View { @State private var fruits = ["Apple", "Banana", "Cherry"] var body: some View { VStack { List(fruits, id: \.self) { fruit in Text(fruit) } Button("Add Fruit") { fruits.append("Orange") } .padding() } } } @main struct MyApp: App { var body: some Scene { WindowGroup { ContentView() } } }
OutputSuccess
Important Notes
Lists update automatically when the data changes if you use @State or other reactive properties.
Using unique IDs helps SwiftUI track which rows changed for smooth updates.
Common mistake: forgetting to provide an ID causes errors or wrong updates.
Summary
Lists show dynamic content that can change size and items.
Use lists when you want to display many items that update over time.
Provide unique IDs so the list updates correctly and smoothly.