SwiftReorder
SwiftReorder copied to clipboard
App is crashing when dragging custom cell to a new section
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
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()
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.
Yes I was updating data model in tableView(_:UITableView, reorderRowAt:IndexPath, to:IndexPath)
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.
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?
@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.
@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?
@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.
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! 🙂