I am making an iPhone-first app which has a 2 level data structure. I want the user to be able to drag List rows from section to section, but not reorder the sections. Is there a SwiftUI way to do it, or should I resort to UIKit?
The basic problem seems to be that ForEach.onMove only allows reordering within its nearest datasource. That seems sensible as a general facility.
As it stands now drags can only happen within a section.
Swift
ForEach(groups) { group in
Section(header: Text(group.text)) {
ForEach(group.items) { item in
Text(item.text)
}
}
}
.onMove { … }
I might be willing to dive into UIKit to get this done, if it'll handle it. Right now I've flattened the data and marked some items as "containers" and others as "leaves". All get emitted as a dumb flat list:
Swift
ForEach(items) { item in
if item.isContainer {
Text(item.text).bold()
}
else {
Text(item.text)
}
}
.onMove { … }
The major downside is users can move the container "section header" lines.
Drag and drop doesn't seem to work on iPhone, just iPads. I'm kinda shy about hierarchical lists using List(_,children:) because I expect move would still allow top level items (my containers) would be allowed to be moved.
4
0
5.6k