Multiple Selection In List - iPadOS 17

Good morning,
Not getting any reaction from the list :
@State var multiSelection = Set()
NavigationView {
List (instruments, selection: $multiSelection) { instrument in
Text(instrument.name)
}
.toolbar { EditButton() }
}
The list does present and the edit button does show but the list doesn’t react - show picker circle not seen .
Following Apple documentation for List. Assuming the @Environment(/.editMode) is built into the list ?
Any comments are appreciated
Joel

I found an example online that works:

struct Book: Identifiable, Hashable {
    let name: String
    let id = UUID()
}

struct ContentView: View {
    @State private var multiSelection = Set<UUID>()

    var books = [
        Book(name: "SwiftUI"),
        Book(name: "Swift"),
        Book(name: "Objective-C"),
        Book(name: "C#"),
        Book(name: "Java"),
        Book(name: "SwiftUI"),
        Book(name: "Swift"),
        Book(name: "Objective-C"),
        Book(name: "C#"),
        Book(name: "Java")
    ]

    var body: some View {
        NavigationStack {
            List(books, selection: $multiSelection) { book in
                Text(book.name)
            }
            .navigationTitle("Books")
            .toolbar { EditButton() }
        }
        Text("\(multiSelection.count) selected")
    }
}

Note that multiSelection is declared as = Set<UUID>()

When you add example code to your post you need to select that code block and format it as a code block by tapping the </> button in the editing tool bar at the top of the editing window. What that does is place 3 back-ticks ``` above the block of code and 3 back-ticks ``` below the block of code.

Thanks for quick response Chris. i’ve replicated these examples online
Did find a “mistake” I used NavigationView instead of NavigationStack – However still had the same non results Not even greying of the row

@State var multiSelection = Set()

NavigationStack {
List (instruments,selection: $multiSelection) { instrument in
Text(instrument.name)
}
.toolbar {EditButton()}

            }

Just setting up to test if it is a problem with working in a sheet . That’s all I can think of in being different from the online examples. I know the multiselection/editmode changed in iOS 16 but that should allow row selection without edimode. If the sheet is a problem will try gesture/swipe to move rows from listA to list B as I still want to use a sheet for input.
Joel

Good morning, just tested sample code with a sheet - works .

OK so what were you doing previously? I don’t quite understand where the problem was in your App.

The NavigationView you had previously is not really going to cause you an issue. It would still have worked in the example I provided you. I just changed it to NavigationStack since that is the preferred option for iOS.

Still trying to figure that out . My list is not reacting with my code that replicates the example. Tested with .onTapgesture which shows the list is accepting taps
However received the error < <0x101215190> Gesture: System gesture gate timed out.

I think the issue is the model `

@Model
class Instrument {
    
    @Attribute(.unique) let id: String
    var name: String
    var instrumentDescription: String
    var inventory: Bool
    var catagory: String

    init(id: String, name: String, instrumentDescription: String, inventory: Bool, catagory: String) {
        self.id = UUID().uuidString
        self.name = name
        self.instrumentDescription = instrumentDescription
        self.inventory = inventory
        self.catagory = catagory
    
    }
}

When I used UUID for the id it generated a runtime error , but the string worked . I think this messes up the list methods for multi selection

If your id property is a String then change your multiSelection property declaration to
@State private var multiSelection = Set<String>()

That makes perfect sense . Tested and works !
Thanks
Joel

1 Like