【问题标题】:Swift Ui - Different View(Layout) for first item of a ListSwiftui - 列表第一项的不同视图(布局)
【发布时间】:2020-04-12 18:03:33
【问题描述】:

我对@9​​87654321@ 比较陌生。我想实现weatherList 的第一项以不同的视图显示。

不幸的是,我在这 2 行 (VStack and at ForEach-Line) 中收到此错误。如果我把if i == 0 收起来,然后只用于所有listitems 的正常布局,就没有错误。

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

这是我的代码:

var body: some View {

    NavigationView {

        VStack { // here first error

            if self.model.error != nil {

                Text(self.model.error!)

            }

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



                if i == 0 {

                    NavigationLink(destination: DetailView(model: self.model.weatherList[i])) {

                        RowView(model: self.model.weatherList[i])

                    }

                }

                else{

                    NavigationLink(destination: DetailView(model: self.model.weatherList[i])) {

                        RowView(model: self.model.weatherList[i])

                    }

                }

            }



        }

        .navigationBarTitle(Text("Weather in " + UserSettings.instance.locationSetting))

        .navigationBarItems(trailing:

            Button(action: {

                self.model.reload()

            }) {

                Image(systemName: "arrow.clockwise")

            }

            .disabled(model.reloading)

        )

    }

}

不胜感激每一个建议。谢谢。

【问题讨论】:

    标签: list swiftui


    【解决方案1】:

    我还没有尝试过,但是如果你像这样包装 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])
            })
        }
    

    【讨论】:

      【解决方案2】:

      在没有其他代码来编译的情况下确定它有点棘手,但我在本地模拟了它,看起来你需要更改的只是:

                  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])
                      }
                  }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-09-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多