不管是使用代理還是使用通知傳遞高度的值蓬抄,要改邊tableviewcell的高度丰嘉,都需要刷新表,但是這個表刷新嚷缭,又要導致webview代理方法的運行饮亏,這樣形成一個死循環(huán)
在cell.m文件里面
//這個方法是在webview請求成功的時候走的,(如果該方法不走耍贾,說明請求不成功)在此方法中獲取webview的內容高度
(void)webViewDidFinishLoad:(UIWebView *)webView { // float height = [[webView stringByEvaluatingJavaScriptFromString:@"document.body.offsetHeight"] floatValue]; //此方法獲取webview的內容高度,但是有時獲取的不完全 // float height = [webView sizeThatFits:CGSizeZero].height; //此方法獲取webview的高度 float height = [[webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.scrollHeight"]floatValue]; //此方法獲取webview的內容高度(建議使用) //設置通知或者代理來傳高度 [[NSNotificationCenter defaultCenter]postNotificationName:@"getCellHightNotification" object:nil userInfo:@{@"height":[NSNumber numberWithFloat:height]}]; } //該方法是在請求失敗的時候走的路幸,如果請求不成功荐开,可以在此打印失敗信息 -(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error { NSLog(@"%@",error); }
在- (void)viewDidLoad方法里面接受通知
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(setTableViewCellHight:) name:@"getCellHightNotification" object:nil];
實現(xiàn)通知中的方法(在此防止死循環(huán)) -(void)setTableViewCellHight:(NSNotification )info { NSDictionarydic=info.userInfo; //判斷通知中的參數(shù)是否與原來的值一致,防止死循環(huán) if (_height != [[dic objectForKey:@"height"]floatValue]) { _height=[[dic objectForKey:@"height"]floatValue]; [self.tableView reloadData]; } }