I am using a UITableView
to display a couple of sections which represent a to-do item, each with a couple of rows, which represent a smaller to-do item:
- Finish thesis
-- Interview representatives
-- Rewrite chapter 8
-- Create cover sheet
- Clean the house
-- Do the dishes
-- Mop living room
-- Clean windows in bathroom
I have successfully implemented a way to rearrange the rows by using the DragDelegate
and DropDelegate
of UITableView
. I make sure everything is moveable (protocol canMoveRowAt
returns true). To give you an idea of how I do this:
func tableView(_ tableView: UITableView, itemsForBeginning session: UIDragSession, at indexPath: IndexPath) -> [UIDragItem] {
let toDoName = toDoList[indexPath.section].toDoItem[indexPath.row].toDoName
let itemProvider = NSItemProvider(object: toDoName as NSString)
let dragItem = UIDragItem(itemProvider: itemProvider)
dragItem.localObject = item
return [dragItem]
}
In MoveRowAt()
I make sure the data is changed accordingly, removing the data from the sourceIndexPath
and inserting it in the destinationIndexPath
. This works great.
However, I would like to make sections draggable as well - to sort the to do list on priority. For example, if I add a new to do item, it automatically is appended to the bottom, but maybe this is something important that needs to be done first, so I want it - as a section with all its rows - to be dragged up if needed.
Can I accomplish this with the same method? I have figured out that there are no protocols for sections as there are for rows (moveRowAt
exists, but moveSectionAt does not, same for canMoveSectionAt).
I have found a function, tableView.moveSection(section, toSection: section)
, which I will probably need to use. But how would I implement the drag & drop and attach it to the header of the section? Do I need to use a custom UILongPressGestureRecognizer
? Any tips or available libraries on how to accomplish this?