简体   繁体   中英

SwiftUI List selection has no value

I want to basically make didSelectRow like UITableView in SwiftUI.

This is the code:

struct ContentView: View {

    var testData = [Foo(name: "1"),
                Foo(name: "2"),
                Foo(name: "3"),
                Foo(name: "4"),
                Foo(name: "5")]

    @State var selected: Foo?

    var body: some View {

        NavigationView {
        VStack {
            List(testData, id: \.name, selection: $selected) { foo in
                HStack {
                    Text(foo.name)

                }
            }.navigationBarTitle("Selected \(selected?.name ?? "")")
            Button("Check:") {
                print(selected?.name)
            }
        }

    }
}

I was thought if I click the cell then selected should contains the selected value, but it's not. The selected has no value. And the cell not clickable.

So I added a Button .

NavigationView {
        VStack {
            List(testData, id: \.name, selection: $selected) { foo in
                HStack {
                    Text(foo.name)
                    Button("Test") {
                        print("\(foo) is selected.")
                        print(selected?.name)
                    }
                }
            }.navigationBarTitle("Selected \(selected?.name ?? "")")
            Button("Check:") {
                print(selected?.name)
            }
        }

Now, click works, but actually foo is the one I want there's no need selected why selection of the List is here.

Not sure anything I missed. Should the Button is necessary for the List " didSelectRow "? thanks!

EDIT

After a bit more investigation, my current conclusion is:

  • For single selections, no need call List(.. selection:) . But you have to use Button or OnTapGesture for clickable.
  • List(.. selection:) is only for edit mode, which is multiple selection, as you can see the selection: needs a set. My example should be @State var selected: Set<Foo>?

On iOS selection works in Edit mode by design

    /// Creates a list with the given content that supports selecting multiple
    /// rows.
    ///
>>    /// On iOS and tvOS, you must explicitly put the list into edit mode for
>>    /// the selection to apply.
    ///
    /// - Parameters:
    ///   - selection: A binding to a set that identifies selected rows.
    ///   - content: The content of the list.
    @available(watchOS, unavailable)
    public init(selection: Binding<Set<SelectionValue>>?, @ViewBuilder content: () -> Content)

so you need either add EditButton somewhere, or activate edit mode programmatically, like

List(selection: $selection) {
   // ... other code
}
.environment(\.editMode, .constant(.active))   // eg. persistent edit mode

Update: Here is some demo of default SwiftUI List selection

演示

struct DemoView: View {
    @State private var selection: Set<Int>?
    @State private var numbers = [0,1,2,3,4,5,6,7,8,9]
    var body: some View {
        List(selection: $selection) {
            ForEach(numbers, id: \.self) { number in
                VStack {
                    Text("\(number)")
                }
            }.onDelete(perform: {_ in})
        }
        .environment(\.editMode, .constant(.active))
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM