Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any feasible way to use UIWebView/WKWebView to load dynamic content in a UITableViewCell?

I'm building an RSS Reader for iPhone where I get articles and their HTML from the server and I want to display this list of articles for the user in a UITableView.

I've explored options like DTCoreText to be able to use HTML with a UILabel or UITextView, but it unfortunately just doesn't offer rich enough support of all the HTML I could be dealing with. So UIWebView or WKWebView seem like my only options for accurately representing the content.

The problem lies with how content having varying height. Detecting the content's height isn't too bad, but when you use it with a UITableView, the table view asks for the heights of all the rows first, and with web views you have to load the content first, and then in the callback for having finished loaded, only then can you find out the height. Which is annoying, because in my case the HTML is simple enough that it takes very little time for the web view to render it.

So you have this issue where the table view asks for the height of a row, and you basically say "Just a second, take this random value for now and I'll tell you in a second when I find out." This works terribly, because when you're scrolling through the table it takes the intermediate value, then loads content after and updates, causing a jump, which is especially bad if you're scrolling up as you're being bounced around as the heights for the rows above you are being calculated. Caching the height the first time you see it doesn't seem to work either, because the user can rotate the device and have that cached value be invalid.

So I'm wondering, is there any way to be able to use web views in a table view?

like image 996
Doug Smith Avatar asked Dec 06 '25 03:12

Doug Smith


1 Answers

You can if you've preloaded the web content. Simply modify your approach and don't tell the table view the rows exist until you have the web content for that row. Then once the content is loaded you can get its height and insert the row at the bottom of the table view. Continue this process until all the content is loaded.

like image 198
InkGolem Avatar answered Dec 08 '25 18:12

InkGolem