博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
博客> UITableViewCell嵌套UIWebView UITableViewCell嵌套UIWebView
阅读量:4578 次
发布时间:2019-06-08

本文共 1658 字,大约阅读时间需要 5 分钟。

1 获取UIWebView高度

- (void)webViewDidFinishLoad:(UIWebView *)webView{    // 如果要获取webView高度必须在网页加载完成之后获取    // 方法一    CGFloat height = [self.webView sizeThatFits:CGSizeZero].height; // 方法二 CGFloat height = webView.scrollView.contentSize.height; // 方法三 (不推荐使用,当webView.scalesPageToFit = YES计算的高度不准确) CGFloat height = [[self.webView stringByEvaluatingJavaScriptFromString:@"document.body.offsetHeight"] floatValue]; }

2 UIWebView加载完成后cell高度的更新,使用通知来实现。

TableViewCell.m

- (void)webViewDidFinishLoad:(UIWebView *)webView{    CGSize fittingSize = [self.webView sizeThatFits:CGSizeZero]; self.height = fittingSize.height; self.webView.frame = CGRectMake(0, 0, fittingSize.width, fittingSize.height); // 用通知发送加载完成后的高度 [[NSNotificationCenter defaultCenter] postNotificationName:@"WEBVIEW_HEIGHT" object:self userInfo:nil]; }

ViewController.m

- (void)viewDidLoad{    [super viewDidLoad];    // 用于缓存cell高度    self.heightDic = [[NSMutableDictionary alloc] init]; // 注册加载完成高度的通知 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(noti:) name:@"WEBVIEW_HEIGHT" object:nil]; } - (void)noti:(NSNotification *)sender { TableViewCell *cell = [sender object]; if (![self.heightDic objectForKey:[NSString stringWithFormat:@"%ld",cell.tag]]||[[self.heightDic objectForKey:[NSString stringWithFormat:@"%ld",cell.tag]] floatValue] != cell.height) { [self.heightDic setObject:[NSNumber numberWithFloat:cell.height] forKey:[NSString stringWithFormat:@"%ld",cell.tag]]; [self.tableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:cell.tag inSection:0]] withRowAnimation:UITableViewRowAnimationNone]; } }

转载于:https://www.cnblogs.com/jingjin/p/6438828.html

你可能感兴趣的文章
过滤器、监听器、拦截器的区别
查看>>
为什么要进行需求分析?通常对软件系统有哪些需求?
查看>>
一些模板
查看>>
jquery和dom元素相互转换
查看>>
放大的X--HDOJ-201307292012
查看>>
题目831-签到-nyoj-20140818
查看>>
百词斩-斩家秘籍
查看>>
Mysql主从配置,实现读写分离
查看>>
完整版本的停车场管理系统源代码带服务端+手机android客户端
查看>>
排序精讲
查看>>
【bzoj3172】 Tjoi2013—单词
查看>>
【uoj2】 NOI2014—起床困难综合症
查看>>
js return的用法
查看>>
子节点填充父元素除去一固定高度后的剩余高度
查看>>
[原]IOS 后台发送邮件
查看>>
(转)JAVA Calendar详解
查看>>
转: 编码,charset,乱码,unicode,utf-8与net简单释义
查看>>
C#--正则匹配
查看>>
5.30 考试修改+总结
查看>>
BA-设计施工调试流程
查看>>