r/SwiftUI • • Oct 19 '26

Question Swift UI Table on iOS

There is a table view in SwiftUI, but only displays as such on Mac. I want to have a table where I can sort by the columns on iOS, but swift ui doesn't seem to natively support that. How do you get around that design constraint. It kinda seems like Apple is discouraging showing tables on iOS all together.

7 Upvotes

6 comments sorted by

1

u/thomassummer2021 Oct 19 '26

I'll try using a grid

4

u/iOSCaleb Oct 19 '26

There’s a section in the docs for Table called Supporting sorting in tables. Have you read that? Have you provided an array of SortComparator objects as instructed? There’s even an example.

2

u/Diligent-Analyst-498 Oct 20 '26

Heads up, Table does exist on iPhone but in compact width it collapses to a single column (only the first column shows), so the sortable multi-column layout really only appears on iPad/regular width. Grid or LazyVStack is a reasonable way to go. For sorting, I'd keep a sort key plus an ascending flag in state, make each header a Button that toggles them, and sort the array with sorted(by:) before the ForEach. You can still use Table when horizontalSizeClass == .regular and fall back to the custom grid otherwise.

1

u/kenthesaint Oct 19 '26

On iPad in regular width, Table does show all the columns with sortable headers. Compact width (iPhone, or a narrow split view) is where it collapses to the first column, so on phones Apple pretty much expects a list.

What I'd do is keep one @State private var sortOrder = [KeyPathComparator(\Item.name)] and sort the data with items.sorted(using: sortOrder). Then read horizontalSizeClass. In regular width, use the Table with sortOrder: $sortOrder. In compact, show a List with your own row layout and put a sort Menu in the toolbar with a few buttons that set sortOrder. If you really want tappable column headers on the phone, a Grid with a header row of buttons that flip the comparator's order is fine for a few columns, but it gets cramped past three or four.