UITableViewCell嵌套UIWebView
来源:互联网 发布:java 延时 编辑:程序博客网 时间:2024/05/20 13:13
原文地址点击打开链接
项目需求:UITableViewCell嵌套UIWebView,并且让UIWebView根据内容自适应高度。
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]; }}
3 完整项目工程
https://github.com/YiQieSuiYuan/Demo/tree/master/WebView_cell
4 参考
http://www.jianshu.com/p/e199496a8b8a
阅读全文
1 0
- UITableViewCell嵌套UIWebView
- UITableViewCell嵌套UIWebView
- UITableViewCell嵌套UIWebView
- UITableViewCell嵌套UIWebView
- UITableViewCell嵌套UIWebView
- UITableViewCell 嵌套 UIWebView,cell自适应高度
- iOS学习笔记:UITableViewcell 嵌套 UIWebView
- iOS UITableViewCell嵌套UIWebView 自适应高度
- UITableViewCell中嵌套UICollectionView
- UITableViewCell中嵌套UICollectionView
- iOS Cell嵌套UIWebView(内附UIWebView详解)
- UITableViewCell嵌套UITableView的正确姿势
- 关于UItableViewCell上加载UIWebView自适应高度问题
- UITableViewCell中嵌套UITableView,用UITextView加载HTML数据
- uitableviewcell怎么取字典里面嵌套字典的key??
- uitableviewcell怎么取字典里面嵌套字典的key??
- UIScrollView内嵌套UIWebView时,因为无法取得UIWebView的实际高度无法滚动的问题
- UITableViewCell
- 关于Solr/ES,我们不得不知道的十件事
- Java 构造函数不可以继承,因此不能被重写,但可以被重载
- 华为上机编程题‘‘移动棋子’’
- 初始化Java对象的详细过程
- 高斯滤波及高斯卷积核C++实现
- UITableViewCell嵌套UIWebView
- heap和stack的区别
- Thrift交流(一)简单的Thrift
- Android开源项目:GifView——Android显示GIF动画
- layout_alignParentBottom失效
- Nutch基础教程(2.3.1版本)--Nutch2.3.X爬虫任务概述
- MFC对话框如何运行后隐藏起来
- expdp和impdp存在于不同数据库不同表空间不同用户之间
- 多线程编程10例问题(2)