article

Sunday, August 15, 2021

SwiftUI Move Rows from List

SwiftUI Move Rows from List

ContentView.swift
 
//
//  ContentView.swift
//  swiftuidev
//
//  Created by Cairocoders
//

import SwiftUI

struct ContentView: View {
    @State private var numbers = ["One", "Two", "Three", "Four", "Five"]
    
    var body: some View {
        NavigationView {
            List {
                ForEach(numbers, id: \.self) { number in
                    Text(number)
                }
                .onMove { (indexSet, index) in
                    self.numbers.move(fromOffsets: indexSet,
                                    toOffset: index)
                }
            
            .navigationBarTitle(Text("Move Rows From List"))
            }
            .navigationBarItems(trailing: EditButton())
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

Related Post