You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -4,35 +4,124 @@ Learn how to provide data to your table using the type-safe API.
4
4
5
5
## Overview
6
6
7
-
SwiftDataTables uses a type-safe API with `DataTableColumn<T>` to define your table structure. This approach provides automatic diffing, animated updates, and compile-time safety.
7
+
SwiftDataTables works directly with your model types. Define columns using KeyPaths, pass your array of models, and the table handles the rest - including animated updates when your data changes.
8
8
9
-
## Basic Usage
9
+
The key requirement: your models must conform to `Identifiable`. This allows SwiftDataTables to track individual rows and animate only what changed.
10
10
11
-
Define columns using KeyPaths, then pass your model array:
11
+
## Making Your Models Identifiable
12
+
13
+
If your model already has a unique identifier, conforming to `Identifiable` is straightforward:
12
14
13
15
```swift
14
16
structPerson: Identifiable {
15
-
let id: Int
17
+
let id: Int// Already have a unique ID? You're done.
18
+
let name: String
19
+
let email: String
20
+
let department: String
21
+
}
22
+
```
23
+
24
+
For models without a natural ID, add one:
25
+
26
+
```swift
27
+
structProduct: Identifiable {
28
+
let id =UUID() // Generate a unique ID
16
29
let name: String
17
-
letage: Int
18
-
letcity: String
30
+
letprice: Double
31
+
letcategory: String
19
32
}
33
+
```
34
+
35
+
> Important: The `id` property enables diffing. Without it, SwiftDataTables can't determine which rows changed, which means no animated updates and no scroll position preservation.
36
+
37
+
## Defining Columns
20
38
39
+
Use KeyPaths to map model properties to columns:
40
+
41
+
```swift
21
42
let columns: [DataTableColumn<Person>] = [
22
43
.init("Name", \.name),
23
-
.init("Age", \.age),
24
-
.init("City", \.city)
44
+
.init("Email", \.email),
45
+
.init("Department", \.department)
25
46
]
26
47
27
48
let dataTable =SwiftDataTable(columns: columns)
49
+
```
28
50
29
-
// Load data later
51
+
The compiler verifies your KeyPaths at build time - typos are caught immediately.
For data without a predefined model (CSV files, JSON responses, database queries), create a wrapper struct:
122
+
Sometimes you're working with data that doesn't have a predefined model - CSV files, JSON responses, or database queries where the schema isn't known at compile time.
123
+
124
+
For these cases, create a simple wrapper struct:
36
125
37
126
### CSV Import
38
127
@@ -50,7 +139,7 @@ func loadCSV(from url: URL) {
50
139
// Create columns dynamically
51
140
let columns: [DataTableColumn<CSVRow>] = headers.enumerated().map { index, header in
0 commit comments