NEW: Get your ticket for Hacking with Swift Live 2019! >>

< How to let users delete rows from a list   How to enable editing on a list using EditButton >

How to let users move rows in a list

SwiftUI gives us simple hooks into lists to let us move rows around, although some of the functionality that was demonstrated at WWDC isn’t actually available in the current beta so we need a workaround.

What does work is that we can attach an onMove(perform:) modifier to items in a list, and have it call a method of our choosing when a move operation happens. That method needs to accept a source IndexSet and a destination Int, like this:

func move(from source: IndexSet, to destination: Int) {

When moving several items it’s always a good idea to move the later ones first so that you avoid moving other items and getting your indexes confused.

As an example, we could create a ContentView struct that sets up an array of three username strings, and asks SwiftUI to move them around calling a move() method. In order to activate moving – i.e., to make the drag handles appear – it also adds an edit button to the navigation view so the user can toggle editing mode.

Here’s the code:

struct ContentView : View {
    @State var users = ["Paul", "Taylor", "Adele"]

    var body: some View {
        NavigationView {
            List {
                ForEach(users.identified(by: \.self)) { user in
                    Text(user)
                }
                .onMove(perform: move)
            }
            .navigationBarItems(trailing: EditButton())
        }
    }

    func move(from source: IndexSet, to destination: Int) {
        // sort the indexes low to high
        let reversedSource = source.sorted()

        // then loop from the back to avoid reordering problems
        for index in reversedSource.reversed() { 
            // for each item, remove it and insert it at the destination
            users.insert(users.remove(at: index), at: destination)
        }
    }
}

In the WWDC session demos their move() method was just one line of code because it used an extension on Swift’s arrays that isn’t available to us – hopefully it will arrive soon!

HACKING WITH SWIFT LIVE This July is a new two-day event where you'll be inspired by great speakers on day one then learn all the amazing new features from WWDC on day two – click here for more information and tickets.

< How to let users delete rows from a list   How to enable editing on a list using EditButton >
MASTER SWIFT NOW
Buy Testing Swift Buy Practical iOS 12 Buy Pro Swift Buy Swift Design Patterns Buy Swift Coding Challenges Buy Server-Side Swift (Vapor Edition) Buy Server-Side Swift (Kitura Edition) Buy Hacking with macOS Buy Advanced iOS Volume One Buy Advanced iOS Volume Two Buy Hacking with watchOS Buy Hacking with tvOS Buy Hacking with Swift Buy Dive Into SpriteKit Buy Swift in Sixty Seconds Buy Objective-C for Swift Developers Buy Beyond Code

Was this page useful? Let me know!

Click here to visit the Hacking with Swift store >>