Skip to content
Data Table preview
Rendered from the SwiftUI source on this page.

Data Table

From syxUI — written for both platforms, not translated between them.

A plain data table with a header row and aligned columns.

Cards · SwiftUI · iOS 17

  • cards
  • swiftui

The actual source

TableBasic.swift
// Data Table · syxUI · https://syxui.dev/components/table-basic
// Free in any project. Keep this line and credit syxUI where a person can read it.

import SwiftUI

extension View {
    func tblCard(_ w: CGFloat) -> some View {
        self.frame(width: w)
            .background(Color(.systemBackground), in: RoundedRectangle(cornerRadius: 18, style: .continuous))
            .clipShape(RoundedRectangle(cornerRadius: 18, style: .continuous))
            .overlay(RoundedRectangle(cornerRadius: 18, style: .continuous).stroke(Color.primary.opacity(0.08), lineWidth: 1))
            .shadow(color: .black.opacity(0.04), radius: 10, x: 0, y: 4)
    }
}

/// Plain data table - header row + product/qty/price columns, hairline row rules.
struct TableBasic: View {
    let rows: [(String, String, String)] = [
        ("MacBook Air", "3", "$999"), ("AirPods Pro", "8", "$249"),
        ("Magic Mouse", "5", "$79"), ("USB-C Cable", "24", "$19"),
    ]
    var body: some View {
        VStack(spacing: 0) {
            HStack {
                Text("Product").frame(maxWidth: .infinity, alignment: .leading)
                Text("Qty").frame(width: 50, alignment: .trailing)
                Text("Price").frame(width: 64, alignment: .trailing)
            }
            .font(.system(size: 12, weight: .semibold)).foregroundStyle(.secondary)
            .padding(.horizontal, 16).padding(.vertical, 11)
            Divider()
            ForEach(Array(rows.enumerated()), id: \.offset) { i, r in
                HStack {
                    Text(r.0).font(.system(size: 14, weight: .medium)).frame(maxWidth: .infinity, alignment: .leading)
                    Text(r.1).font(.system(size: 14).monospacedDigit()).foregroundStyle(.secondary).frame(width: 50, alignment: .trailing)
                    Text(r.2).font(.system(size: 14, weight: .semibold).monospacedDigit()).frame(width: 64, alignment: .trailing)
                }
                .padding(.horizontal, 16).padding(.vertical, 12)
                if i < rows.count - 1 { Divider().padding(.leading, 16) }
            }
        }
        .tblCard(320)
    }
}
iOS 17 · No dependencies

Dependencies

SwiftUI
No external dependencies

Requires iOS 17