In an earlier post, we went over what a ForEach loop is used for. In this tutorial, we’ll look at the List.
A List is designed to show a collection of views in a table format, much like how you see your emails, contacts, music and so on. This is similar to the UITableView in concept, but how it is implemented is very different.
If you have read the ForEach loop tutorial, the last example showed how to put four capsules or pills horizontally on the screen. Here is the code we used for this:
struct TagListView: View {
let discussionPoints = [
DiscussionPoint(title: "Meeting Agenda", tag: "Important"),
DiscussionPoint(title: "Budget Review", tag: "Finance"),
DiscussionPoint(title: "Project Updates", tag: "Development"),
DiscussionPoint(title: "Team Building", tag: "HR")
]
var body: some View {
HStack {
ForEach(discussionPoints) { point in
Text(point.tag)
.padding(.horizontal, 12)
.padding(.vertical, 6)
.background(Capsule().fill(Color.blue.opacity(0.2)))
.foregroundColor(.blue)
}
}
}
}
struct DiscussionPoint: Identifiable {
let id = UUID()
let title: String
let tag: String
}