Monday, November 11, 2019

Avoid Wrong Height UITableView When Reload and Add New Row


I have a problem where I can't find the solution on the internet like google, stackoverflow and blogs. So I need to trick this so that It work like I want. It's like the bug from iOS because what's supposed to be i'm doing it's the correct way. And the error seems weird because it's only sometimes happened.
I try to find it by way and it takes long time. And finally I got the solution but I think it's not final solution but I hope iOS will fix the issue soon.

So the error it's when you first time entering UITableview.  If you dynamic height like instagram did, you will sometimes get wrong height. But if it's not you are saved. I don't know if this problem only happened to me or not but after a few years expierence. I think this bug happened not only me.

My solution is not the best but I think it's really good to hide the pain because of this annoying bug haha. What I did is to refresh three times refresh when first reload UITableview. So if you have opened the UITableview before don't need to recall it again. Because it will make the memory bad.



So this is the code


So basically we need to call setNeedsLayout and layoutIfNeeded first because we the layout is not good when first time opened. And the first row need to reload again because the height is not correct for the very first time.

Remember this bug happened when you use Automatic UITableViewAutomaticDimension and you use UIImageView and the height is dynamic not static. Because if your height is static this bug will be ok.

So If you have the same issue with and you found good solution please comment below. I will put in my article blog. Thanks for visiting this simple blog. And I'm sorry if my grammar english is not really good. Have a nice day all...

No comments:

Post a Comment