SwiftUI allows us to make static table views by using a List view. The SwiftUI syntax makes the process extremely simple. Unlike UITableViewControllers that require you to manually specify that you are using static cells, and then have to assign the table view controller a class, you simply just add “List { }” to your code and insert all of the views you want in-between the curly braces.
The basic syntax is as follows:
struct ContentView: View {
var body: some View {
List {
Text("Cell 1")
Text("Cell 2")
Toggle(isOn: .constant(true)) {
Text("Cell 3")
}
Text("Cell 4")
Text("Cell 5")
Text("Cell 6")
}
}
}
All you need to do is embed each cell within the List. In the example code above, you will see a standard looking table view with 6 static cells that display text, and one of them shows a toggle. You can change out the Text views for anything you want, including stacks if needed.
This type of view works great for settings screens where you don’t have repeated information. Please note that there is a limit of 10 views that can be embedded in a List.
Leave a Reply
You must be logged in to post a comment.