I am trying to learn SwiftUI by converting my personal Android apps. I am initially prototyping the interfaces. I have coded a LazyVGrid. I want to be able to tap on a “cell” and be able to determine the Id that I assigned to that cell/view or the row and column of that cell/view in order to manipulate the data displayed. This app is to run on an iPhone so, to my understanding, a table would not display all of my columns.
”’
import SwiftUI
struct ContentView: View {
init() {
let appearance = UINavigationBarAppearance()
appearance.largeTitleTextAttributes = [
.font: UIFont.systemFont(ofSize: 30)]
UINavigationBar.appearance().standardAppearance = appearance
}
private var columns = [GridItem(.fixed(120), spacing: 0),
GridItem(.fixed(52), spacing: 0),
GridItem(.fixed(52), spacing: 0),
GridItem(.fixed(52), spacing: 0),
GridItem(.fixed(52), spacing: 0),
GridItem(.fixed(52), spacing: 0)]
@State var gridData: [String] = []
var body: some View {
NavigationStack {
ScrollView {
LazyVGrid(columns: columns, spacing: 0) {
ForEach((0..<gridData.count), id: .self) {
let width: CGFloat = $0%6 == 0 ? 120 : 52
let alignment: Alignment = $0%6 == 0 ? .leading : .center
Text(gridData[$0]).padding(.horizontal, 4)
.frame(width: width, height: 30, alignment: alignment)
.border(Color.black)
}
}.onTapGesture {
print("tapped ")
}
}.task {
gridData = setupGridData()
}.navigationTitle("Tee Time Administration").fontWeight(.light)
.toolbar {
ToolbarItem(placement: .navigationBarTrailing) {
Image(systemName: "gear")
}
}
}
}
”’