TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

Drag and Drop in a List

Forums > SwiftUI

(oops! I tried to reply to my post, and replaced it instead - sorry.)

Okay, well, ahem.

I got this to work with a List. Which I always understood would not work, that you would have to use ForEach. By using a List, I get access to the Hierachical sidebar list that I need.

The issue was my Model data class (Sidebar) gave it indigestion. There were NO error messages, there was NO crash. It simply hung. No memory leaks, I could find no where to place a breakpoint - it was basically off comtemplating it's navel.

To fix it I finally, fixed it by dragging only the sidebar.id (a uuidString) and then in the destination, doing a "if let sidebar = sidebars.first(where: {$0.id == items[0]}) { print(sidebar.title) }"

Here is my List for those that might be struggling with doing this:

                    List(sidebars.sorted{$0.index < $1.index}[0].children!, id:\.id, children: \.children, selection: $selected) { sidebar in
                        NavigationLink {
                            SidebarDetailSelectorView(sidebar: sidebar)
                        } label: {
                            rowLabel(sidebar: sidebar)
                            //.background(Color.random())
                        }
                        .onChange(of: selected) {
                            current.sidebar = sidebars.first{ $0.id == selected }
                        }
                        .draggable(sidebar.id)
                        .dropDestination(for: UUID.self) { items, location in
                            if let sidebar = sidebars.first(where: {$0.id == items[0]}) {
                                print(sidebar.title)
                                // do magic here...
                            }
                            return true
                        } isTargeted: { isTargeted in
                            sidebar.isTargeted = isTargeted
                        }
                    }

3      

Hacking with Swift is sponsored by Blaze.

SPONSORED Still waiting on your CI build? Speed it up ~3x with Blaze - change one line, pay less, keep your existing GitHub workflows. First 25 HWS readers to use code HACKING at checkout get 50% off the first year. Try it now for free!

Reserve your spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.