I am trying to rearrange List rows when in edit mode.
Quick summary: I want DisclosureGroup rows like Second to not be inserted into their own children's hierarchy. It's fine if they go into any other hierarchy/level.
Below is some example code:
struct ContentView: View {
var body: some View {
List {
ForEach(0 ..< 1) { _ in
DisclosureGroup("First", isExpanded: .constant(true)) {
ForEach(0 ..< 1) { _ in
Text("Item 1")
Text("Item 2")
DisclosureGroup("Second", isExpanded: .constant(true)) {
// Individual
Text("Item 3")
Text("Item 4")
// Grouped
// ForEach(0 ..< 1) { _ in
// Text("Item 3")
// Text("Item 4")
// }
}
}
}
}
.onMove(perform: { _, _ in })
}
.listStyle(.sidebar)
.environment(\.editMode, .constant(.active))
}
}
This is what the line comments mean:
- Individual: this will allow the rows to move freely within the current level, but also means the
DisclosureGrouprow can move between these elements (not wanted) - Grouped: this will 'lock' these views into this level of the hierarchy. For example
Item 3andItem 4can be swapped rows, however these rows can no longer be dragged somewhere else such as betweenItem 1andItem 2.
When a DisclosureGroup row is dragged, either:
- All the children should be dragged along with it.
- This
DisclosureGrouprow should not be inserted between any of its own children.
We are purely focusing on visual UI for now, hence why the onMove(perform:) method is not completed.
Notes:
Individual method: The
Secondrow can be moved to the same layer asItem 3andItem 4, which should not be allowed. However, it does allowItem 3andItem 4to be betweenItem 1andItem 2.Grouped method:
Item 3andItem 4are 'locked' into their own level - no views can be inserted between, nor can they move to the same level asItem 1andItem 2(bad). However, in this caseSecondcannot move into its own children's hierarchy (good).
How can I fix this, so I basically have the 'individual method', but DisclosureGroup rows like Second cannot be inserted into their own children's hierarchy? It's fine if they go into any other hierarchy/level.
I realise this is quite a complex question - so try the code for yourself and watch the GIFs to understand.
If it functions correctly, I'll accept a UIKit solution too (such as with UICollectionView?).

