我正在开发一个应用程序,该应用程序要根据用户选择显示数据的年份来显示数据。
使用ForEach时,我目前无法最终确定选择-当我手动编写数据时,它可以完美地工作。我想要的是更改选定年份的颜色,否则将其保持灰色。
我认为从State更改为ObservedObject是可以的,但是没有用。当前,当我单击年份时,它被选中,但其他未被选中(在某种意义上,它们保持黑色而不是灰色)–当我打印ObservedObject值而不是年份时值,每个值都不同。
import SwiftUI
struct YearSelection: View {
var body: some View {
ScrollView(.horizontal, showsIndicators: false) {
HStack(spacing: 34) {
ForEach(YearData) { item in
YearSelectionList( year: item)
}
Spacer()
}
}.padding(.bottom)
}
}
struct YearSelectionList: View {
var year: Year
@ObservedObject var selectedYear = SelectedYear()
var body: some View {
Button(action: {selectedYear.selectedYear = year.year}, label: {
Text(year.year).foregroundColor(selectedYear.selectedYear == year.year ? Color.black : Color.gray)
})
}
}
class SelectedYear: ObservableObject {
@Published var selectedYear = "2021"
}
struct Year: Identifiable {
var id = UUID()
var type: String
var year: String
}
let YearData = [
Year(type: "1", year: "2021"),
Year(type: "1", year: "2017"),
Year(type: "1", year: "2013"),
Year(type: "1", year: "2010"),
Year(type: "1", year: "2006"),
Year(type: "1", year: "2002")
]
谢谢您的帮助!
答案 0 :(得分:0)
import SwiftUI
struct ContentView: View {
var body: some View {
YearSelection()
}
}
struct Year: Identifiable
{
var id = UUID()
var selected: Bool
var year: String
}
var YearData = [
Year(selected: false, year: "2021"),
Year(selected: false, year: "2017"),
Year(selected: false, year: "2013"),
Year(selected: false, year: "2010"),
Year(selected: false, year: "2006"),
Year(selected: false, year: "2002")
]
class SelectedYear: ObservableObject
{
@Published var selectedYear = YearData
}
struct YearSelection: View
{
@ObservedObject var selectedYear = SelectedYear()
var body: some View
{
ScrollView(.horizontal, showsIndicators: false)
{
HStack(spacing: 34)
{
ForEach(selectedYear.selectedYear.indices) { index in
Button(action: {
if selectedYear.selectedYear.count > 0
{
for i in 0 ... selectedYear.selectedYear.count - 1
{
selectedYear.selectedYear[i].selected = false
}
}
selectedYear.selectedYear[index].selected.toggle()
}){
Text(selectedYear.selectedYear[index].year).foregroundColor(selectedYear.selectedYear[index].selected ? Color.red : Color.gray)
}
}
Spacer()
}
}
.padding(.bottom)
}
}
版本2.0.0:
import SwiftUI
struct ContentView: View {
var body: some View {
YearSelection()
}
}
struct Year: Identifiable
{
var id = UUID()
var selected: Bool
var year: String
}
var YearData = [
Year(selected: false, year: "2021"),
Year(selected: false, year: "2017"),
Year(selected: false, year: "2013"),
Year(selected: false, year: "2010"),
Year(selected: false, year: "2006"),
Year(selected: false, year: "2005"),
Year(selected: false, year: "2004"),
Year(selected: false, year: "2003"),
Year(selected: false, year: "2002")
]
class SelectedYear: ObservableObject
{
@Published var selectedYear = YearData
}
struct YearSelection: View
{
@ObservedObject var selectedYear = SelectedYear()
@State var previousSelectedYear: Int = -1
var body: some View
{
ScrollView(.horizontal, showsIndicators: false)
{
HStack(spacing: 34)
{
ForEach(selectedYear.selectedYear.indices) { index in
Button(action: {
if (previousSelectedYear != -1) { selectedYear.selectedYear[previousSelectedYear].selected = false }
selectedYear.selectedYear[index].selected.toggle()
previousSelectedYear = index
}){ Text(selectedYear.selectedYear[index].year).foregroundColor(selectedYear.selectedYear[index].selected ? Color.red : Color.gray) }
}
}
}
}
}