Skip to content

Instantly share code, notes, and snippets.

@PaulWoodIII
Created August 19, 2019 00:02
Show Gist options
  • Save PaulWoodIII/c67f6351ec7a336e5853fcaa3980df3a to your computer and use it in GitHub Desktop.
Save PaulWoodIII/c67f6351ec7a336e5853fcaa3980df3a to your computer and use it in GitHub Desktop.
quick example of how to use selection in a SwiftUI list, an ObservableObject provides the content as well
//: [Previous](@previous)
import SwiftUI
import Combine
import PlaygroundSupport
class Context: ObservableObject {
@Published var selectedItems: Set<String>
@Published var items: Array<String>
init() {
self.items = ["A","B","C"]
self.selectedItems = ["A"]
}
}
extension String: Identifiable {
public var id: String { return self }
}
struct SelectedItems: View {
@ObservedObject var context: Context
var body: some View {
NavigationView {
List (context.items, selection: $context.selectedItems) { item in
Text(item)
}.environment(\.editMode, .constant(.active))
}
}
}
let selectedItems = SelectedItems(context: Context())
let viewController = UIHostingController(rootView: selectedItems)
PlaygroundPage.current.liveView = viewController
//: [Next](@next)
@malhal
Copy link

malhal commented Aug 26, 2020

Shame List selection binding is Mac only

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment