Created
August 19, 2019 00:02
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//: [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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Shame List selection binding is Mac only