I feel like this might be a common issue and was wondering if there was any common solution to it.
Basically, my UITableView has dynamic cell heights for every cell. If I am not at the top of the UITableView and I tableView.reloadData()
, scrolling up becomes jumpy.
I believe this is due to the fact that because I reloaded data, as I'm scrolling up, the UITableView is recalculating the height for each cell coming into visibility. How do I mitigate that, or how do I only reloadData from a certain IndexPath to the end of the UITableView?
Further, when I do manage to scroll all the way to the top, I can scroll back down and then up, no problem with no jumping. This is most likely because the UITableViewCell heights were already calculated.
@Igor answer is working fine in this case,
Swift-4
code of it.in following methods of
UITableViewDelegate
I have tried all the workarounds above, but nothing worked.
After spending hours and going through all the possible frustrations, figured out a way to fix this. This solution is a life savior! Worked like a charm!
Swift 4
I added it as an extension, to make the code look cleaner and avoid writing all these lines every time I want to reload.
finally ..
OR you could actually add these line in your
UITableViewCell
awakeFromNib()
methodand do normal
reloadData()
Overriding the estimatedHeightForRowAtIndexPath method with an high value, for example 300f
This should fix the problem :)
Try to call
cell.layoutSubviews()
before returning cell infunc cellForRowAtIndexPath(_ indexPath: NSIndexPath) -> UITableViewCell?
. It's known bug in iOS8.I ran into this today and observed:
cellForRowAtIndexPath
doesn't help.The fix was actually pretty simple:
Override
estimatedHeightForRowAtIndexPath
and make sure it returns the correct values.With this, all weird jittering and jumping around in my UITableViews has stopped.
NOTE: I actually know the size of my cells. There are only two possible values. If your cells are truly variable-sized, then you might want to cache the
cell.bounds.size.height
fromtableView:willDisplayCell:forRowAtIndexPath:
I use more ways how to fix it:
For view controller:
as the extension for UITableView
The result is