Updated for Xcode 12.5
SwiftUI’s list view has built-in support for sections and section headers, just like UITableView
in UIKit. To add a section around some cells, start by placing a Section
around it, optionally also adding a header and footer.
As an example, we could create a row that holds task data for a reminders app, then create a list view that has two sections: one for important tasks and one for less important tasks.
Here’s how that looks:
struct TaskRow: View {
var body: some View {
Text("Task data goes here")
}
}
struct ContentView: View {
var body: some View {
List {
Section(header: Text("Important tasks")) {
TaskRow()
TaskRow()
TaskRow()
}
Section(header: Text("Other tasks")) {
TaskRow()
TaskRow()
TaskRow()
}
}
}
}
You can also add footer text to sections, like this:
List {
Section(header: Text("Other tasks"), footer: Text("End")) {
Text("Row 1")
Text("Row 2")
Text("Row 3")
}
}
SPONSORED ViRE offers discoverable way of working with regex. It provides really readable regex experience, code complete & cheat sheet, unit tests, powerful replace system, step-by-step search & replace, regex visual scheme, regex history & playground. ViRE is available on Mac & iPad.
Sponsor Hacking with Swift and reach the world's largest Swift community!
Link copied to your pasteboard.