1
import SwiftUI

struct ContentView: View {

  let disciplines = ["Potato", "Tomato", "Onion"]

  var body: some View {
        NavigationView{
            List(disciplines, id: \.self) { discipline in
                NavigationLink(
                    destination: DetailView(discipline: discipline)) {
                        Text(discipline)
                    }
            }
            .navigationBarTitle (Text("The App Title"), displayMode: .inline)
        }
    }
}



struct DetailView: View {
  let discipline: String
  var body: some View {
    Text(discipline)
  }
}


struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

例如,现在,当我在列表中点击“土豆”时,视图中的文本将显示“土豆”。但我希望列出的每个文本都有不同的文本,而不是同一个词。

如何使用 SwiftUI 做到这一点?

谢谢你。

4

2 回答 2

1

您可以创建一个ingredient对象并拥有两个字段。一个用于name一个用于description

struct Ingredient: Identifiable {
    var id = UUID()
    var name: String
    var description: String
}

有了这个,您必须将您的修改disciplines为以下内容以获取ingredient对象而不是字符串

    let disciplines = [Ingredient(name: "Potato", description: "Yummy potato"),
                       Ingredient(name: "Tomato", description: "Yummy Tomato"),
                       Ingredient(name: "Onion", description: "Yummy Onion")]

并且您的视图会稍微改变以使用对象中的nameanddescription字段ingredient

    var body: some View {
        NavigationView{
            List(disciplines) { discipline in
                NavigationLink(
                destination: DetailView(discipline: discipline.description)) {
                    Text(discipline.name)
                }
            }
            .navigationBarTitle (Text("The App Title"), displayMode: .inline)
        }
    }
于 2020-02-26T21:02:16.687 回答
0

在下面的示例中,我使用了MVVM概念来分离视图及其逻辑。接下来,ViewModel 包含两个数组/字典:

1. disciplinesIds持有唯一的 id 来识别学科
2 。disciplines保存要显示的项目,映射到 id

import SwiftUI

struct discipline {
    let name: String
    let content: String
}

final class ContentViewModel: ObservableObject {
    @Published var disciplinesIds: [UUID] = []
    @Published var disciplines: [UUID : discipline] = [:]

    init() {
        for item: String in ["Potato", "Tomato", "Onion"] {
            let id = UUID()

            self.disciplinesIds.append(id)
            self.disciplines[id] = discipline(name: item, content: item + "_content")
        }
    }
}


struct ContentView: View {
    @ObservedObject var viewModel: ContentViewModel

    var body: some View {
        NavigationView{
            List(self.viewModel.disciplinesIds, id: \.self) { discipline in
                NavigationLink(destination: DetailView(discipline: self.viewModel.disciplines[discipline]!.content)) {
                    Text(self.viewModel.disciplines[discipline]!.name)
                }
            }
            .navigationBarTitle (Text("The App Title"), displayMode: .inline)
        }
    }
}

struct DetailView: View {
    let discipline: String

    var body: some View {
        Text(discipline)
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView(viewModel: .init())
    }
}

在您的视图中,为了提高性能(请参阅本文)而不是使用字典本身,遍历 id 列表。您甚至可以将discipline对象传递给视图DetailView

于 2020-02-26T21:03:11.713 回答