ForEach 在 SwiftUI LazyVGrid 中检索错误的值
ForEach is retrieving wrong value in SwiftUI LazyVGrid
我正在开发带有 ForEach 循环的 SwiftUI LazyVGrid。我想向 LazyVGrid 中的每个项目添加按钮,然后根据用户选择处理编辑、查看或删除。删除按钮似乎工作正常,但用于编辑和查看的 NavigationLink 似乎 return ForEach 循环中的随机对象。
var body: some View {
GeometryReader { geo in
VStack {
ScrollView {
LazyVGrid(columns: gridLayout, alignment: .center, spacing: 1) {
ForEach(events, id: \.self) { event in
HStack {
VStack {
ZStack {
Spacer()
Image(uiImage: (event.cardFrontImage ?? blankCardFront)!)
HStack {
VStack {
Spacer()
Text("\(event.event ?? "")")
}
}
VStack {
HStack {
Spacer()
NavigationLink(destination: EditAnEvent(event: event, recipient: recipient), isActive: $isEditActive, label: {
Image(systemName: "square.and.pencil")
})
NavigationLink(destination: CardView(cardImage: (event.cardFrontImage ?? blankCardFront)!, event: event.event ?? "Unknown Event", eventDate: event.eventDate! as Date), isActive: $isCardActive, label: {
Image(systemName: "doc.text.image")
})
Button(action: {
areYouSure.toggle()
}, label: {
Image(systemName: "trash")
})
.confirmationDialog("Are you Sure", isPresented: $areYouSure, titleVisibility: .visible) {
Button("Yes") {
withAnimation {
print("Deleting Event \(String(describing: event.event))")
deleteEvent(event: event)
}
}
Button("No") {
withAnimation {
print("Cancelled delete of \(String(describing: event.event))")
}
}
}
}
}
}
}
}
.frame(height: geo.size.width * 0.3)
.background(Color(.systemGray5))
.overlay(
RoundedRectangle(cornerRadius: 20)
.stroke(Color.white, lineWidth: 20)
)
}
}
}
}
}
.accentColor(.green)
我删除了尽可能多的格式以减少行数,并演示了问题。
分解视图应该有助于修复它。例如。将删除按钮和确认放在自己的视图中。
我正在开发带有 ForEach 循环的 SwiftUI LazyVGrid。我想向 LazyVGrid 中的每个项目添加按钮,然后根据用户选择处理编辑、查看或删除。删除按钮似乎工作正常,但用于编辑和查看的 NavigationLink 似乎 return ForEach 循环中的随机对象。
var body: some View {
GeometryReader { geo in
VStack {
ScrollView {
LazyVGrid(columns: gridLayout, alignment: .center, spacing: 1) {
ForEach(events, id: \.self) { event in
HStack {
VStack {
ZStack {
Spacer()
Image(uiImage: (event.cardFrontImage ?? blankCardFront)!)
HStack {
VStack {
Spacer()
Text("\(event.event ?? "")")
}
}
VStack {
HStack {
Spacer()
NavigationLink(destination: EditAnEvent(event: event, recipient: recipient), isActive: $isEditActive, label: {
Image(systemName: "square.and.pencil")
})
NavigationLink(destination: CardView(cardImage: (event.cardFrontImage ?? blankCardFront)!, event: event.event ?? "Unknown Event", eventDate: event.eventDate! as Date), isActive: $isCardActive, label: {
Image(systemName: "doc.text.image")
})
Button(action: {
areYouSure.toggle()
}, label: {
Image(systemName: "trash")
})
.confirmationDialog("Are you Sure", isPresented: $areYouSure, titleVisibility: .visible) {
Button("Yes") {
withAnimation {
print("Deleting Event \(String(describing: event.event))")
deleteEvent(event: event)
}
}
Button("No") {
withAnimation {
print("Cancelled delete of \(String(describing: event.event))")
}
}
}
}
}
}
}
}
.frame(height: geo.size.width * 0.3)
.background(Color(.systemGray5))
.overlay(
RoundedRectangle(cornerRadius: 20)
.stroke(Color.white, lineWidth: 20)
)
}
}
}
}
}
.accentColor(.green)
我删除了尽可能多的格式以减少行数,并演示了问题。
分解视图应该有助于修复它。例如。将删除按钮和确认放在自己的视图中。