Skip to content

Instantly share code, notes, and snippets.

@yousifalraheem
Created May 17, 2021 10:39
Show Gist options
  • Save yousifalraheem/e62b3a7b55299040e538ac17c724d9e6 to your computer and use it in GitHub Desktop.
Save yousifalraheem/e62b3a7b55299040e538ac17c724d9e6 to your computer and use it in GitHub Desktop.
A TableView component for SwiftUI
import SwiftUI
struct TableRow: Identifiable {
let id = UUID()
let cells: [AnyView]
}
struct TableView: View {
let headers: [String]
let rows: [TableRow]
var body: some View {
HStack {
ForEach(0..<headers.count) { columnCount in
if (columnCount != 0) {
Spacer()
}
VStack {
Text(headers[columnCount])
.fontWeight(.bold)
.padding(.vertical)
ForEach(0..<rows.count) { rowCount in
rows[rowCount].cells[columnCount]
}
}
}
}
.padding(.horizontal)
}
}
struct TableView_Previews: PreviewProvider {
static var previews: some View {
TableView(headers: [], rows: [])
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment