Skip to main content

swiftui-list-and-section | ios-app-dev-memo

swiftui List .. selection

自前で contents 書く場合

swift
@State private var selectedDispType: DispType? = .listByArea

List(selection: $selectedDispType) {
Label("By Area" , systemImage: "globe.asia.australia") .tag(DispType.listByArea)
Label("By Item" , systemImage: "figure.play") .tag(DispType.listByItem)
Label("Preference" , systemImage: "gearshape") .tag(DispType.preference)
}

'init(selection:content:)' is unavailable in iOS こんなエラーが出るかもしれないが、それは selection の引数に渡す Binding 変数を optional にしてないだけかも

普通はリストで ForEach 相当すると思うので、このように。

swift
List(books, id: \.persistentModelID, selection: $selectedBookID) { book in
Text("\(book.title)")
}