Please help. The code is hard to show but basically I have made a view of a list of buttons from an array in my json file. My problem is that whenever I change the property of the button in my view (like the color) it changes the color of every button. And I can’t assign an action to the button individually. Is there a way that I can use the index number from the items in the array to highlight an individual button and to have each button execute individual action when pressed?
Todd
import SwiftUI
struct PE: View {
var spacing: CGFloat = 8
var padding: CGFloat = 8
var wordCount: Int = 75
var alignmentIndex = 0
var poswords: [String] {
Array(p.posPE.prefix(wordCount))
}
var negwords: [String] {
Array(p.negPE.prefix(wordCount))
}
let alignments: [HorizontalAlignment] = [.leading, .center, .trailing]
var alignment: HorizontalAlignment {
alignments[alignmentIndex]
}
@State private var note: String = ""
var p:Pizza
var body: some View {
Text(p.name)
.font(.largeTitle)
.multilineTextAlignment(.leading)
.padding(.vertical)
// Positive findings
SwiftUI.ScrollView {
FlexibleView(
data: poswords,
spacing: spacing,
alignment: alignment
){ topping in
Button(topping) {}
.padding()
.clipShape(Capsule())
.foregroundColor(Color.black)
.background(Color.green.opacity(0.2))
.font(.caption)
.cornerRadius(13.0)
}
}
.padding()
Divider()
// Negative findings
SwiftUI.ScrollView {
FlexibleView(
data: negwords,
spacing: spacing,
alignment: alignment
){ topping in
Button(topping) {}
.padding()
.clipShape(Capsule())
.foregroundColor(Color.black)
.background(Color.red.opacity(0.2))
.font(.caption)
.cornerRadius(13.0)
}
.padding()
if #available(iOS 15.0, *) {
TextField(
"Additional notes",
text: $note
)
.padding()
.border(.secondary)
} else {
// Fallback on earlier versions
}
}
}
}