Currently, I create a generic list as follows:
List {
ForEach(viewModel.categoryItems) { category in
Text("(category.category)")
}.onMove { indexSet, newOffset in
viewModel.categoryItems.move(fromOffsets: indexSet, toOffset: newOffset)
}
}.toolbar {
ToolbarItem {
if viewModel.categoryItems.count > 0 {
EditButton()
}
}
}
I would like to be able to number this list and also have those numbers change on move (2 becomes 1 if moved into the 1 place).
2
Answers
one option would be to use the
indices
property of the array. Something like this:Downside is that you have to access the object via the array as you only get the current index within the loop.
Some more elaborate solutions are listed here: How to get row index in SwiftUI List?
Give your Category Item a
nr
property. Then in.onMove
after the move itself just renumber, e.g. like this: