SwiftReorder
SwiftReorder copied to clipboard
Reordering dynamically sized cells causes content to jump around significantly
This occurs when the content size is larger than the table view bounds. Probably an issue with cell height calculations.
Great one @adamshin. I think the issue is the cells move the movement of the cells then trigger another move which causes a loop. I believe similar logic to whats here might fix this up although I could be wrong. https://github.com/Raizlabs/LXReorderableCollectionViewFlowLayout/blob/master/LXReorderableCollectionViewFlowLayout/LXReorderableCollectionViewFlowLayout.m#L157
Thanks for the tip, I'll take a look at that.
@ay8s Spent some time debugging this today -- unfortunately, it appears to be a UIKit issue. 😞 Calling insertRows(at:with:)
or deleteRows(at:with:)
on a table view with dynamically-sized cells causes the content offset to shift around unpredictably. Seems like it's related to the estimated row height you give.
I wasn't able to come up with a satisfactory solution, so I'm going to table this for now. In the meantime, a workaround would be to calculate cell heights manually (the pre-iOS 8 way).
just do this in viewcontroller to fix this jumping:
-
var cellHeights = NSMutableDictionary()
-
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat { if let height = cellHeights.object(forKey: indexPath) { return height as! CGFloat } return UITableViewAutomaticDimension
}
-
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) { cellHeights.setObject(cell.frame.size.height, forKey: indexPath as NSCopying)
}
This fixed the issue on my side.
Is there a way to implement these changes within the source itself so I don't have to add all this to each of my tableViews?
I'm using extension for UITableView - https://github.com/APUtils/APExtensions/blob/master/APExtensions/Classes/Core/_Extensions/UITableView%2BUtils.swift#L82
Then it's just one line of code: tableView.handleEstimatedSizeAutomatically = true
Fixed in https://github.com/adamshin/SwiftReorder/pull/67