
- SwiftUI - Home
- SwiftUI - Overview
- SwiftUI vs UIkit
- SwiftUI Environment
- SwiftUI - Environment Setup
- SwiftUI - Basic Components
- SwiftUI - Building First Application
- SwiftUI Views
- SwiftUI - Views
- SwiftUI - Customize Text View
- SwiftUI - Custom Image View
- SwiftUI - Stacks
- SwiftUI Drawing Shapes
- SwiftUI - Shapes
- SwiftUI - Drawing line
- SwiftUI - Drawing Rectangle
- SwiftUI - Drawing Rounded Rectangle
- SwiftUI - Drawing Triangle
- SwiftUI - Drawing Circle
- SwiftUI - Drawing Star
- SwiftUI - Drawing Polygon
- SwiftUI - Drawing Pie chart
- SwiftUI - Using built-in shapes
- SwiftUI - Text
- SwiftUI - Text View
- SwiftUI - Text Input and Output
- SwiftUI - Color
- SwiftUI - Color
- SwiftUI - Colorpicker
- SwiftUI - Gradients
- SwiftUI - Adjust Color
- SwiftUI - Effects
- SwiftUI - Effects
- SwiftUI - Blend Effect
- SwiftUI - BLur Effect
- SwiftUI - Shadow Effect
- SwiftUI - Hover Effect
- SwiftUI - Animations
- SwiftUI - Animations
- SwiftUI - Creating Animations
- SwiftUI - Creating an Explicit Animation
- SwiftUI - Multiple Animations
- SwiftUI - Transitions
- SwiftUI - Asymmetric Transition
- SwiftUI - Custom Transition
- SwiftUI - Image
- SwiftUI - Images
- SwiftUI - Image as Background
- SwiftUI - Rotating Image
- SwiftUI - Media
- SwiftUI - View Layout
- SwiftUI - View Layout
- SwiftUI - View Size
- SwiftUI - View Spacing
- SwiftUI - View Padding
- SwiftUI - UI Controls
- SwiftUI - UI Controls
- SwiftUI - Button
- SwiftUI - CheckBox
- SwiftUI - Menubar
- SwiftUI - Toolbar
- SwiftUI - Search Bar
- SwiftUI - TextField
- SwiftUI - Slider
- SwiftUI - Toggle
- SwiftUI - Pickers
- SwiftUI - Menus
- SwiftUI - List & Tables
- SwiftUI - Lists
- SwiftUI - Static List
- SwiftUI - Dynamic List
- SwiftUI - Customize List
- SwiftUI - Tables
- SwiftUI - Forms
- SwiftUI - Forms
- SwiftUI - Breaking Forms in Sections
- SwiftUI - Event Handling
- SwiftUI - Event Handling
- SwiftUI - Gesture
- SwiftUI - Clipboard
- SwiftUI - Drag and Drop
- SwiftUI - Focus
- SwiftUI - Alert
- SwiftUI - Miscellaneous
- SwiftUI - Containers
- SwiftUI - Navigation
- SwiftUI - Notifications
- SwiftUI - Cross-Platform UI
- SwiftUI - Data
- SwiftUI - Accessibility
- SwiftUI - Framework Integration
- SwiftUI - Framework Integration
- SwiftUI - Interfacing with UIKit
- SwiftUI - Creating macOS App
- SwiftUI Useful Resources
- SwiftUI - Useful Resources
- SwiftUI - Discussion
SwiftUI - Tables
SwiftUI provide a good support to tables. A table view is used to display data in tabular form. It can create simple as well as complex table layouts. It allows us to display in a structured and multi-column layout.
We are also allowed to customize a table and its data such as header, style of table content, font, background color, formatting of each column, etc. Calendar, product comparison, etc are some real life examples of tables in UI. So, in this chapter, we are going to cover how to create a table, how to style, how to sort table data and much more.
Creating Table in SwiftUI
In SwiftUI, we can create a table with the help of Table View. Table view displays data in rows and column format. It computes its rows based on an identifiable data collection. Identifiable is a class whose instance holds the value of an entity with a valid identity. The table supports both horizontal and vertical scrolling to adjust the view of the table if the number of rows and columns exceeds the width of the view.
Syntax
Following is the syntax −
Table(IdentifiableData){ TableColumn(Text, value: \.IdentifiableProperty1) TableColumn(Text, value: \.IdentifiableProperty1 }
Example
The following SwiftUI program is used to create a simple table which stores the names of the students and their favorite subjects.
import SwiftUI struct Student: Identifiable{ let name: String let id = UUID() let subject: String } struct ContentView: View { @State private var stud = [ Student(name: "Mohina", subject: "Maths"), Student(name: "Rohita", subject: "Science"), Student(name: "Soman", subject: "Maths"), Student(name: "Soha", subject: "Science"), ] var body: some View { VStack{ Text("Class = 1 Data ").font(.title2) // Creating table Table(stud){ TableColumn("Student Names", value: \.name) TableColumn("Favourite Subject", value: \.subject) } } } } #Preview { ContentView() }
Output

Adding Selection to the Table in SwiftUI
We can also make one or more rows of the table selectable by creating a binding selection variable of Set type. To create a single instance of the table data selectable, we have to create an id type for that table and then pass that variable in the Table view.
Example
The following SwiftUI program is used to create a selectable table.
import SwiftUI struct Student: Identifiable{ let name: String let id = UUID() let age: Int let subject: String } struct ContentView: View { @State private var stud = [ Student(name: "Mohina", age: 19, subject: "Maths"), Student(name: "Rohita", age: 18, subject: "Science"), Student(name: "Soman", age: 10, subject: "Maths"), Student(name: "Soha", age: 17, subject: "Science"), ] @State private var select = Set() var body: some View { VStack{ Text("Original Table ").font(.title2) // Creating table Table(stud, selection: $select){ TableColumn("Student Names", value: \.name) TableColumn("Age"){ stud in Text(String(stud.age)) } TableColumn("Favourite Subject", value: \.subject) } } } } #Preview { ContentView() }
Output

Styling Table in SwiftUI
In SwiftUI, we can style the table according to the need of the design using the tableStyle() modifier. The .tableStyle() modifier provides us with some pre-defined styles for the table using them we can enhance the appearance of the simple table.
Syntax
Following is the syntax −
func tableStyle(_ style: S) -> some View where S : TableStyle
It takes only one parameter that is the style. The value of this parameter can be any one of the following −
.automatic: It is used to apply the default system style to the table.
inset: It is used to apply inset style to the table which makes its content indented from the edges.
Example
The following SwiftUI program is used to style the table in inset format.
import SwiftUI struct Student: Identifiable{ let name: String let id = UUID() let age: Int let subject: String } struct ContentView: View { @State private var stud = [ Student(name: "Mohina", age: 19, subject: "Maths"), Student(name: "Rohita", age: 18, subject: "Science"), Student(name: "Soman", age: 10, subject: "Maths"), Student(name: "Soha", age: 17, subject: "Science"), ] var body: some View { VStack{ Text("Original Table ").font(.title2) // Creating table Table(stud){ TableColumn("Student Names", value: \.name) TableColumn("Age"){ stud in Text(String(stud.age)) } TableColumn("Favorite Subject", value: \.subject) }.tableStyle(.inset) } } } #Preview { ContentView() }
Output
