for anyone who might have the same question/problem. here's the sample that works as I wanted.
import SwiftUI
struct ContentView: View {
let numberOfRows = 5
let numberOfColumns = 5
@State var matrix: [[Int]] = Array(repeating: Array(repeating: 0, count: 5), count: 5)
@State var selectedRow1 = 0
@State var selectedColumn1 = 0
@State var selectedRow2 = 0
@State var selectedColumn2 = 0
@State var selectedRow3 = 0
var body: some View {
VStack {
Image(systemName: "globe")
.imageScale(.large)
.foregroundColor(.accentColor)
Text("Hello, world!")
Form {
Section(header: Text("Question 1")) {
Picker("Select a row:", selection: $selectedRow1) {
ForEach(1...numberOfRows, id: \.self) { row in
Text("\(row)")
}
}
.pickerStyle(SegmentedPickerStyle())
Picker("Select a column:", selection: $selectedColumn1) {
ForEach(1...numberOfColumns, id: \.self) { column in
Text("\(column)")
}
}
.pickerStyle(SegmentedPickerStyle())
}
Section(header: Text("Question 2")) {
Picker("Select a row:", selection: $selectedRow2) {
ForEach(1...numberOfRows, id: \.self) { row in
Text("\(row)")
}
}
.pickerStyle(SegmentedPickerStyle())
Picker("Select a column:", selection: $selectedColumn2) {
ForEach(1...numberOfColumns, id: \.self) { column in
Text("\(column)")
}
}
.pickerStyle(SegmentedPickerStyle())
}
Section(header: Text("Question 3")) {
Picker("Select a row:", selection: $selectedRow3) {
ForEach(1...numberOfRows, id: \.self) { row in
Text("\(row)")
}
}
.pickerStyle(SegmentedPickerStyle())
Picker("Select a column:", selection: $selectedColumn3) {
ForEach(1...numberOfColumns, id: \.self) { column in
Text("\(column)")
}
}
.pickerStyle(SegmentedPickerStyle())
}
}
Button(action: {
resetMatrix()
matrix[selectedRow1 - 1][selectedColumn1 - 1] += 1
matrix[selectedRow2 - 1][selectedColumn2 - 1] += 1
matrix[selectedRow3 - 1][selectedColumn3 - 1] += 1
let selectedValue1 = matrix[selectedRow1 - 1][selectedColumn1 - 1]
let selectedValue2 = matrix[selectedRow2 - 1][selectedColumn2 - 1]
let selectedValue3 = matrix[selectedRow3 - 1][selectedColumn3 - 1]
print("Selected Coordinate (Question 1): (\(selectedRow1), \(selectedColumn1))")
print("Value at Coordinate (Question 1): \(selectedValue1)")
print("Selected Coordinate (Question 2): (\(selectedRow2), \(selectedColumn2))")
print("Value at Coordinate (Question 2): \(selectedValue2)")
print("Selected Coordinate (Question 3): (\(selectedRow3), \(selectedColumn3))")
print("Value at Coordinate (Question 3): \(selectedValue3)")
}) {
Text("Show Selected Coordinates")
.padding()
.background(Color.blue)
.foregroundColor(.white)
.cornerRadius(10)
}
.padding()
VStack {
ForEach(0..<numberOfRows, id: \.self) { row in
HStack {
ForEach(0..<numberOfColumns, id: \.self) { column in
let value = matrix[row][column]
Text("\(value)")
.frame(width: 50, height: 50)
.border(Color.gray)
.id("\(row)-\(column)")
}
}
}
}
}
.padding()
}
func resetMatrix() {
matrix = Array(repeating: Array(repeating: 0, count: numberOfColumns), count: numberOfRows)
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}