- 本文實現(xiàn)的效果是在某個TableViewCell上加載UIWebView,加載完成后高度正常顯示.
1.屬性
@property (nonatomic, strong) UIWebView *webView;
if (!_webView) {
//[DMDevceManager screenWidth]是屏幕寬度,100是先預(yù)留出加載動畫的高度
_webView = [[UIWebView alloc]initWithFrame:CGRectMake(0, 0, [DMDevceManager screenWidth], 100)];
_webView.delegate = self;
_webView.scrollView.scrollEnabled = NO;
}
//加載方法,預(yù)先加載url
[self.webView loadRequest:[[NSURLRequest alloc] initWithURL:[NSURL URLWithString:_model.b_url]]];
2.TableView代理方法中的處理
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
...
return _webView.frame.size.height;
...
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
...
if(indexPath.section == 3)
{
static NSString *identifier = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (!cell){
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
[cell.contentView addSubview:_webView];
if (!indicator) {//此處添加加載動畫的處理
...
[cell.contentView addSubview:indicator];
...
}
/* 忽略點擊效果 */
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
}
return cell;
}
...
}
#####3.WebView代理方法中的處理--主要用于獲取高度
#pragma mark - UIWebView Delegate Methods
-(void)webViewDidFinishLoad:(UIWebView *)webView
{
//獲取到webview的高度
CGFloat height = [[self.webView stringByEvaluatingJavaScriptFromString:@"document.body.offsetHeight"] floatValue];
self.webView.frame = CGRectMake(self.webView.frame.origin.x,self.webView.frame.origin.y, [DMDevceManager screenWidth], height);
// [SVProgressHUD showSuccessWithStatus:@"圖文詳情加載成功"];
...//此處移除或者暫停加載動畫
_isLoadOver = YES;//該布爾用于啟用加載動畫.當(dāng)為NO時在上面可以開始動畫
[_tableView reloadData];
}
- 以上就可以動態(tài)的將web的高度給cell,在cell上正常顯示web頁面.
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。