3

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?

PennyWise
  • 595
  • 2
  • 12
  • 37

1 Answers1

0

I don't know if it's possible to drag and drop an entire section, but probably not, right now the API can consents to drag and drop row; one thing you could do is to create a CollectionView with a tableView in every Item, so you can drag and drop the Item containing your tableView

  • 1
    Could be a solution, but I don’t think that’s an ideal scenario to have multiple TableViews acting as rows for a CollectionView. I hope there is another way — otherwise I will have to look into it. – PennyWise Nov 26 '18 at 17:06
  • Yeah that's probably not the best solution, but is the only thing that came to my mind! If you find something else tell me because I'm curious about that – Francesco Destino Nov 27 '18 at 08:25
  • 2
    Eventually, I ended up using up and down arrows in my sections which then call moveSection. Not entirely what I was hoping for, but it works fine. If I ever find a great way to move a section using drag and drop, I'll let you know. – PennyWise Dec 07 '19 at 10:45
  • Meanwhile I developed an application that make soccer lineup for matches with drag and drop, you can swap player with an easy gesture, it's a combination of coordinates and event state, not so hard to implement – Francesco Destino Dec 09 '19 at 09:50
  • Feel free to share a bit of code. Does it also work when you drag off-screen? – PennyWise Dec 09 '19 at 11:17
  • Off-screen? You mean from an application to another one?! I just made some custom UIView with coordinates that can be dragged with a long press gesture – Francesco Destino Dec 09 '19 at 11:27
  • No, I mean when you scroll below the visible area of the screen. – PennyWise Dec 09 '19 at 11:28