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

App is crashing when dragging custom cell to a new section #45

Open
manish026 opened this issue Jan 11, 2019 · 9 comments
Open

App is crashing when dragging custom cell to a new section #45

manish026 opened this issue Jan 11, 2019 · 9 comments

Comments

@manish026
Copy link

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'attempt to delete row 2 from section 0 which only contains 2 rows before the update'

Getting this error while dragging 3rd row from 0th section to 1st row of 1st section

@manish026
Copy link
Author

manish026 commented Jan 11, 2019

To fix this call reorderRowAt delegate after deleting the row

tableView.beginUpdates()

    tableView.deleteRows(at: [context.destinationRow], with: .fade)
    
    delegate?.tableView(tableView, reorderRowAt: context.destinationRow, to: newContext.destinationRow)
    
    tableView.insertRows(at: [newContext.destinationRow], with: .fade)
    tableView.endUpdates()

@adamshin
Copy link
Owner

Are you updating your data model in tableView(_:UITableView, reorderRowAt:IndexPath, to:IndexPath)? A couple other people had this issue recently – I left a comment here explaining how to resolve it.

@manish026
Copy link
Author

Yes I was updating data model in tableView(_:UITableView, reorderRowAt:IndexPath, to:IndexPath)

@thejeff77
Copy link

I had a similar issue because somewhere in my code I was calling tableView.endUpdates() to fix a bug with TBEmptyDataSet, and had never called tableView.beginUpdates() - you might check that you have matching beginUpdates and endUpdates calls for any updates you have.

@cliftonlabrum
Copy link

cliftonlabrum commented Mar 10, 2020

I'm getting this same crash. I tried the suggestion of adding the delegate?.tableView... call after deleting the row, but it doesn't work. I've confirmed that I don't have any additional tableView.beginUpdates() calls anywhere. I'm also saving my data model in reorderRowAt.

The issue seems to center around the fact that before you can let go of the row, it has been moved to the new section, saved, and is now attempting to move back to another section next to it in a split second. It's very strange.

I've always wondered why this library saves every time the user drags and doesn't save once when you let go of the row (where it can check the drop destination and then save it). Maybe that's how the original UIKit delegate method works.

Anyone else running into this? Any other ideas for a solution?

@thejeff77
Copy link

@cliftonlabrum perhaps try installing w/ cocoapods and debugging.

I also had a lot of issues with new iOS 13 features with this lib and ended up going over to Apple's Drag/Drop delegates - works great. Had an example project to work with - was pretty convoluted for me to attempt at first without the example.

Good luck.

@cliftonlabrum
Copy link

@thejeff77 Thanks. Are you referring to something other than just the built-in UIKit way of reordering UITableView rows? My goal is to not have the reorder lines/icon that those (used to?) put on a table row since my cells have a custom look.

Do you have a link to that sample project you are referring to?

@thejeff77
Copy link

@cliftonlabrum no I am referring to UIKit drag and drop delegates, although there are ways to customize the drag placeholder view. I'm not sure what you mean by the icons/lines but I know of some ways to remove the 1px separators from the cells. I received the example project from apple code support, so I won't post it but I'll try to get around to emailing it to you. The example doesn't have multiple sections tho.

@cliftonlabrum
Copy link

I found a super helpful Stack Overflow post that shows how easy it is to enable drag and drop for a UITableView. If all you were using this library for was reordering a table without the default reorder control, then this is a simple way to get that working: https://stackoverflow.com/questions/60270449/how-can-i-use-drag-and-drop-to-reorder-a-uitableview

Just add the drag delegates to your table:

tableView.dragDelegate = self
tableView.dragInteractionEnabled = true

...and one delegate method:

func tableView(_ tableView: UITableView, itemsForBeginning session: UIDragSession, at indexPath: IndexPath) -> [UIDragItem] {
  let dragItem = UIDragItem(itemProvider: NSItemProvider())
  return [dragItem]
}

Then use the usual moveRowAt and canMoveRowAt methods. It works great! 🙂

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

4 participants