我有一个项目列表,当用户单击它时,我需要在

问题描述 投票:0回答:1

视图:

struct ContentView: View {
  let arr = [
    Item(name: "Roman", addr: "Address of Roman"),
    Item(name: "Alexa", addr: "Address of Alexa"),
  ]
  @State private var selection: Item?

  var body: some View {
    VStack {
        List {
            ForEach(arr) { item in
                Text(item.name!)
                    .id(item.name!)
                    .onTapGesture {
                        selection = item
                        print("selection: \(item.name ?? "")")
                    }
                    .popover(item: $selection) { item in
                        Text(item.addr ?? "")
                    }
                
            }
        }
     }
  }

用户单击

name
时。弹出案没有出现。
如果列表中只有一个项目,则弹出声如下所示(如下图)。

当弹出案列表列表时,弹出窗口不按预期工作?
这是Swiftui中的错误?
    

您不能在列表中包含弹出式修饰符,因此将其移至列表本身的修饰符

List { ForEach(arr) { item in Text(item.name!) .onTapGesture { selection = item } } } .popover(item: $selection) { item in Text(item.addr ?? "") } enter image description here在附带说明中,不要为Item

的ID使用计算的属性,每次阅读时都会更改。而是使其恒定

struct Item: Identifiable { let id: String = UUID().uuidString //... }

ios swift swiftui swiftui-list
1个回答
0
投票
最新问题
© www.soinside.com 2019 - 2024. All rights reserved.