UITableViewCell嵌套UIWebView
来源:互联网 发布:域名服务器ip查询 编辑:程序博客网 时间:2024/06/04 18:58
我的博客原文地址
项目需求: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
0 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
- 用多个StoryBoard构建工程
- python 实现四则运算(一)
- Storyboard 自定义转场动画
- Oracle表空间介绍
- iOS截屏( UIView,UITableView,UICollectionView,UIWebView)
- UITableViewCell嵌套UIWebView
- iOS近场通信(蓝牙开发,WiFi开发)
- Xcode各版本官方下载链接
- ScrollView与WebView结合使用时,设置webview字体大小的时候出现下方留白解决方案
- UIWebView替代品WKWebView的使用及js交互
- 理解MySQL——架构与概念
- 用js解析经json序列化后的C#的DateTime类型数据
- .NET EF LINQ 分页(Skip/Take)造成数据重复原因
- nodejs实战关于上传图片