UITableViewCell nested UIWebView and cell adaptively based on WebView content

Source: Internet
Author: User

UITableViewCell self-adapting UIWebView height

Nesting UIWebView in UITableViewCell, and let UITableViewCell adaptively height based on content

The first step is to get the height of the UIWebView and introduce three ways

-(void ) Webviewdidfinishload: (UIWebView *) webview{ //  If you want to get webView height, you must obtain the following when the page is loaded /span>//  method one  cgfloat height = [Self.webview sizethatfits:cgsizezero].height;  //  method two  cgfloat height = WebView.scrollView.contentSize.height;  //  method three (deprecated, when webview.scalespagetofit = Yes the height of the calculation is inaccurate)  cgfloat height = [[self.webview stringbyevaluatingjavascriptfromstring:

The second step is to use notifications to update the cell's height after the UIWebView load completes

-(void) Webviewdidfinishload: (UIWebView *) webview{    = [Self.webview sizethatfits: Cgsizezero];     = fittingsize.height;     = CGRectMake (00, fittingsize.width, fittingsize.height);     // Send the height    after loading with notification [[Nsnotificationcenter Defaultcenter] Postnotificationname:@ "webview_height" Object : Self userinfo:nil];}
- (void) viewdidload{[Super Viewdidload]; //used to cache cell HeightsSelf.heightdic =[[Nsmutabledictionary alloc] init]; //register the notification of loading complete height[[Nsnotificationcenter Defaultcenter] addobserver:self selector: @selector (noti:) Name:@"Webview_height" Object: nil];}- (void) Noti: (Nsnotification *) sender{Tableviewcell*cell = [SenderObject]; if(! [Self.heightdic objectforkey:[nsstring stringWithFormat:@"%ld", cell.tag]]| | [[Self.heightdic objectforkey:[nsstring stringWithFormat:@"%ld", Cell.tag]] floatvalue]! =cell.height) {//first, the dictionary that is used for caching does not have the relevant data or is different from the cached data and then caches the new cell height data or updates it .[Self.heightdic setobject:[nsnumber NumberWithFloat:cell.height] forkey:[nsstring stringWithFormat:@"%ld", Cell.tag]];//The reload of the relevant cell is OK.[Self.tableview reloadrowsatindexpaths:@[[nsindexpath IndexPathForRow:cell.tag insection:0] ] [withrowanimation:uitableviewrowanimationnone]; }}

The whole idea is to get the content height of UIWebView and then reload the relevant cell!

UITableViewCell nested UIWebView and cell adaptively based on WebView content

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.