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