问题是因为您正在分配
single binding to multiple popovers
。您应该区分每行的绑定,例如为每行创建不同的绑定:
ForEach(arr) { item in
let binding = Binding<Item?>(
get: { selection == item ? item : nil }, // ð returns the item only for the corresponding row
set: { selection = $0 }
)
Text(item.name!)
.id(item.name!)
.onTapGesture {
selection = item
print("selection: \(item.name ?? "")")
}
.popover(item: binding) { item in // ð Use the specified binding here
Text(item.addr ?? "")
}
}
别忘了做
Item: Equatable
.