Transfer from http://www.jianshu.com/p/e199496a8b8a
New project, encountered a new demand: TableView in a cell nested Web view, want to let Web view according to the content of adaptive height, cell according to WebView adaptive height.
For this requirement, we can complete it in just 2 steps.
1. How to get the height of WebView
WebView's height, I'm from http://stackoverflow.com/questions/7341767/ios-resize-uiwebview-to-fit-content
Http://www.xuebuyuan.com/2035051.html found the answer.
//获取到webview的高度 CGFloat height = [[self.webView stringByEvaluatingJavaScriptFromString:@"document.body.offsetHeight"] floatValue];
2. Get to the height of webview, re-reloadtableview can
- (void) Viewdidload {[Super Viewdidload]; _webview = [[UIWebView Alloc]initwithframe:CGRectMake (0,0, Kscreenwidth,1)]; _webview. Delegate =Self _webview. ScrollView. scrollenabled =NO;Pre-load URL [Self. WebView loadrequest:[[Nsurlrequest Alloc] initwithurl:[Nsurl URLWithString:@ "https://www.baidu.com/"]];} - (void) Didreceivememorywarning {[Super didreceivememorywarning];Dispose of any resources the can be recreated.}#pragma mark-tableviewdelegate & tableviewdatasource-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (Nsinteger) Section {Return the number of the rows in the section.Return3;} - (UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) indexpath{if (Indexpath. row = =1) {StaticNSString *identifier =@ "Cell";UITableViewCell *cell = [TableView dequeuereusablecellwithidentifier:identifier];if (!cell) {cell = [[UITableViewCell Alloc]initwithstyle:Uitableviewcellstyledefault Reuseidentifier:identifier]; [Cell. Contentview Addsubview:_webview];/* Ignore the Click Effect */[cell Setselectionstyle:Uitableviewcellselectionstylenone]; }return cell; }else{StaticNSString *identifier =@ "Cell";UITableViewCell *cell = [TableView dequeuereusablecellwithidentifier:identifier];if (!cell) {cell = [[UITableViewCell Alloc]initwithstyle:Uitableviewcellstyledefault Reuseidentifier:identifier]; } cell. Textlabel. Text = [NSString stringWithFormat:@ "Index====%ld", (Long) Indexpath. row];return cell; }}- (CGFloat) TableView: (UITableView *) TableView Heightforrowatindexpath: (Nsindexpath *) Indexpath; {if (Indexpath. row = =1) {/* When the content height is obtained through the WebView agent, the content height is set to the cell's high */Return _webview. Frame. Size. Height; }else{Return100; }}- (void) TableView: (UITableView *) TableView Didselectrowatindexpath: (Nsindexpath *) indexpath{[TableView deselectrowatindexpath:indexpath animated:YES];}#pragma mark-uiwebview Delegate methods-(void) Webviewdidfinishload: (UIWebView *) webview{Get to the height of the WebViewCGFloat height = [[Self. WebView stringbyevaluatingjavascriptfromstring:@ "Document.body.offsetHeight"] floatvalue];Self. WebView. frame =CGRectMake (Self. WebView. Frame. Origin. x,self. WebView. Frame. Origin. Y, kscreenwidth, height); [self. TableView reloaddata];} -(void) Webviewdidstartload: (UIWebView *) webview{ NSLog (@ "Webviewdidstartload");} -(void) WebView: (UIWebView *) WebView didfailloadwitherror: (Nullable Nserror *) error{ NSLog ( @ "didfailloadwitherror===%@", error);}
Ps
Demo address, please poke here.
Wen/kuazi (author of Jane's book)
Original link: http://www.jianshu.com/p/e199496a8b8a
Copyright belongs to the author, please contact the author to obtain authorization, and Mark "book author".
iOS Development Tableviewcell nested WebView