top of page
Writer's pictureHui Wang

77. SwiftUI: Use List to show content vertically

The List in SwiftUI is like the UITableView in UIKit, which is used to show the contents of an array in the vertical direction.


Make the first List.



List(0 ..< 5) { item in
    Text("Hello World!")
}

It makes a List, and the List has five lines by default.


Make the second List.

  1. Add three Texts to the List to show three records.

List {
    Text("Apple")
    Text("Banana")
    Text("Pear")
}


Next, Make the third List.

  1. Add an array as the List's data source.

  2. Add a ForEach statement to iterate through the array.

  3. Use Text to show strings.

  4. A List will be made based on the data source.

let colors = ["Red", "Green", "Blue"]
var body: some View {
    List {
        ForEach(colors, id: \.self) { color in
            Text(color)
        }
    }
}


 

Follow me on:

Comments


bottom of page