Here are the steps to enable editing for a List
in SwiftUI, and allow the user to delete or move the rows:
EditButton
in the navigation bar of the view. This button allows the user to toggle the editing mode of the List
:main.swift707 chars26 lines
Add the onDelete()
and onMove()
modifiers to the ForEach
inside the List
. These modifiers will be called when the user tries to delete or move a row. In our example, we use them to call the deleteItem()
and moveItem()
functions that update the items
array.
Implement the deleteItem()
and moveItem()
functions to update the items
array according to the action taken by the user. In our example, we use the remove(atOffsets:)
and move(fromOffsets:toOffset:)
methods to manipulate the items
array.
With these steps in place, the user will be able to delete or move rows in the List
by tapping the EditButton
.
gistlibby LogSnag