Swift Ui-列表的第一项的不同视图(布局)

时间:2020-04-12 18:03:33

标签: list swiftui

我对 [HttpGet] `[HttpGet ("{id}")]` 不熟悉。我想完成Swift的第一项显示不同的视图。

不幸的是,我在这两行weatherList中收到此错误。如果我放下(VStack and at ForEach-Line),然后仅将其用于所有if i == 0的常规布局,就没有错误。

listitems

这是我的代码:

ERROR: Type '()' cannot conform to 'View'; only struct/enum/class types can conform to protocols

将感谢每个建议。谢谢。

2 个答案:

答案 0 :(得分:0)

我还没有尝试过,但是如果您像这样包装NavigationLink会怎样:

    if i == 0 {
        AnyView(
            NavigationLink(destination: DetailView(model: self.model.weatherList[i])) {
                RowView(model: self.model.weatherList[i])
        })
    } else {
        AnyView(
            NavigationLink(destination: DetailView(model: self.model.weatherList[i])) {
                RowView(model: self.model.weatherList[i])
        })
    }

答案 1 :(得分:0)

在没有其余代码进行编译的情况下确定要知道有点棘手,但我在本地进行了模拟,看来您需要更改的只是:

            ForEach(0..<self.model.weatherList.count, id: \.self) { i in

            ForEach(0..<self.model.weatherList.count) { i in // remove the id: argument

说明:

如果您使用范围来遍历数组,则不使用id:参数。该范围是可自动识别的。 ForEach(_:id:content:)初始化程序用于您可能会说

的情况
ForEach(self.model.weatherlist, id: \.self) { model in 
    SomeView(model: model)
}

为您编译吗?

编辑:您所给出的示例看起来无论索引为0还是非索引都将执行相同的操作。如果您尝试这样做,为什么没有任何作用?

                ForEach(0..<self.model.weatherList.count) { i in
                NavigationLink(destination: DetailView(model: self.model.weatherList[i])) {
                    RowView(model: self.model.weatherList[i])
                }
            }