Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature Request: Drop cell into another cell #56

Open
thejeff77 opened this issue Jul 26, 2019 · 5 comments
Open

Feature Request: Drop cell into another cell #56

thejeff77 opened this issue Jul 26, 2019 · 5 comments

Comments

@thejeff77
Copy link

thejeff77 commented Jul 26, 2019

Hello again! At some point I'll be putting folders into my table - any suggestions for dragging a cell onto another cell with existing functionality, or places to start to make this possible in your library would be helpful.

I figure additional delegate methods (or a separate delegate) might look something like:

func canDragHoverOverIndexPath(_ indexPath : IndexPath) {
// return true if folder / hoverable element, false otherwise to reorder. Defaults to false if the user doesn't extend/override it.
}

func didDropDragItemIntoIndexPath(_ indexPath: IndexPath, fromSourceIndex sourceIndexPath : IndexPath) {
// delete the source index path and update data. Default to false if the user doesn't extend/override it.
}

@adamshin
Copy link
Owner

That's a good question. I think you'd have to make some substantial changes to the reordering logic. Currently the code is only looking for a new "destination row" as you drag cells around, but to allow hovering, it would have to also check for a "hover" state and update things accordingly. Probably would need to add something like var hoveredRow: IndexPath? to the ReorderContext to keep track of that.

To detect the hover state, it might be as simple as adding a check to updateDestinationRow() before calculating the proposed destination row – i.e., if the dragged cell is almost centered over one of its adjacent neighbors within some tolerance, set that as the hovered row instead of updating the destination row. You'd also need to add logic to endReorder() to handle the case where one cell is dropped into another (delete the source row from the table, notify the delegate, animate the snapshot view away, etc).

Those delegate methods look like a good place to start. You might also want one that fires when a cell is hovered/un-hovered, so the cell can be visually highlighted in some way.

@thejeff77
Copy link
Author

Thanks! I'll definitely need folders at some point, and I don't see a better option than extending this library so far. My high level understanding is that there is a blank cell below the hovered cell that is switching depending on where the dragged cell is located. I might be wrong on that tho.

If that is the case, I assume that I could call my delegate function for a destination row: canDragHoverOverIndexPath - and if it returns true, I'd just delete the blank cell from the table and store the blank cell's reference in a local class. Then I'd set isHighlighted = true on the destination row. This could be problematic however if deleting the blank cell scrolls the tableView, but I'm getting ahead of myself.

Anyway, thanks again for your thoughts on this. I think they will make more sense once I do a side-by-side comparison of the code with your notes.

@adamshin
Copy link
Owner

You're right, there is a blank spacer cell inserted in the tableview beneath the location you're dragging. I don't think you'd want to remove that spacer when hovering over another row though. That would cause your content to keep shifting up and down as you dragged cells around.

As an example, check out the behavior when reordering apps on the iOS home screen. When hovering one app over another to create a folder, there's still a blank space where the original app used to be.

@thejeff77
Copy link
Author

Good feedback. I'll implement it this way. Thanks!

@adamshin
Copy link
Owner

No problem! Let me know if you're able to get it working.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants