Getting Started
Add SwiftDataTables to your project and display your first table in minutes.
Installation
Swift Package Manager (Recommended)
Add SwiftDataTables via Xcode:
- Open your project in Xcode
- Go to File → Add Package Dependencies...
- Enter the repository URL:
https://github.com/pavankataria/SwiftDataTables - Select your version rules and add to your target
Or add it directly to your Package.swift:
Package.swift
dependencies: [
.package(url: "https://github.com/pavankataria/SwiftDataTables", from: "0.9.0")
]Your First Table
Step 1: Import the Framework
import SwiftDataTablesStep 2: Define Your Model
Your model must conform to Identifiable. This enables SwiftDataTables to track individual rows - when you update data, rows animate smoothly (insertions slide in, deletions slide out) instead of the whole table reloading.
struct Employee: Identifiable {
let id: Int // Any Hashable type works: Int, String, UUID, etc.
let name: String
let role: String
let city: String
}
let employees = [
Employee(id: 1, name: "Alice", role: "Engineer", city: "London"),
Employee(id: 2, name: "Bob", role: "Designer", city: "Paris"),
Employee(id: 3, name: "Carol", role: "Manager", city: "Berlin")
]Step 3: Define Columns and Create the Table
Each DataTableColumn defines a column header and how to extract the value from your model. Using key paths (\.name) gives you compile-time safety - typos are caught at build time, not runtime.
let columns: [DataTableColumn<Employee>] = [
.init("Name", \.name), // Header: "Name", Value: employee.name
.init("Role", \.role),
.init("City", \.city)
]
let dataTable = SwiftDataTable(data: employees, columns: columns)Step 4: Add to Your View
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(dataTable)
dataTable.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
dataTable.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
dataTable.leadingAnchor.constraint(equalTo: view.leadingAnchor),
dataTable.trailingAnchor.constraint(equalTo: view.trailingAnchor),
dataTable.bottomAnchor.constraint(equalTo: view.bottomAnchor)
])
}That's it! You now have a fully functional data table with:
- Column sorting (tap headers)
- Built-in search bar
- Automatic column widths
- Row highlighting on selection