Skip to content

Instantly share code, notes, and snippets.

@praeclarum
Created June 28, 2020 00:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save praeclarum/2e2d2f25106b3e2f4138a78b18112efb to your computer and use it in GitHub Desktop.
Save praeclarum/2e2d2f25106b3e2f4138a78b18112efb to your computer and use it in GitHub Desktop.
Data Grid SwiftUI using LazyVGrid
//
// DataGridView.swift
// Predictor
//
// Created by Frank A. Krueger on 6/25/20.
//
import SwiftUI
struct Column: Identifiable {
let id = UUID()
var title: String
var enabled: Bool = true
var rows: [String] = (0..<512).map{x in "Row\(x)"}
}
struct DataGridView: View {
var columns: [Column]
let maxRows = 10000000
struct CellIndex: Identifiable {
let id: Int
let colIndex: Int
let rowIndex: Int
}
var body: some View {
let numCols = columns.count
let numRows = columns[0].rows.count
let columnItems: [GridItem] = Array(repeating: .init(.flexible()), count: numCols)
let cells = (0..<numRows).flatMap{j in columns.enumerated().map{(i,c) in CellIndex(id:j + i*maxRows, colIndex:i, rowIndex:j)}}
return ScrollView {
LazyVGrid(columns:columnItems) {
ForEach(cells) { cellIndex in
let column = columns[cellIndex.colIndex]
HStack {
Spacer()
Text("\(column.title) \(cellIndex.rowIndex) \(column.rows[cellIndex.rowIndex])")
Spacer()
}
}
}
.padding()
.padding(.bottom, 44*4)
}
}
}
struct DataGridView_Previews: PreviewProvider {
static var previews: some View {
let cols = [
Column(title: "A"),
Column(title: "B"),
Column(title: "C"),
Column(title: "D"),
Column(title: "E"),
Column(title: "F"),
Column(title: "G"),
Column(title: "H"),
]
return DataGridView(columns: cols)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment