UITableViewCell嵌套UIWebView

深藏阁楼爱情的钟 2022-07-15 12:49 233阅读 0赞

UITableViewCell嵌套UIWebView

项目需求:UITableViewCell嵌套UIWebView,并且让UIWebView根据内容自适应高度。

1.获取UIWebView高度

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

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

在tableviewCell.m中实现:

  1. - (void)webViewDidFinishLoad:(UIWebView *)webView
  2. {
  3. // 如果要获取web高度必须在网页加载完成之后获取
  4. // 方法一
  5. CGSize fittingSize = [self.webView sizeThatFits:CGSizeZero];
  6. // 方法二
  7. // CGSize fittingSize = webView.scrollView.contentSize;
  8. NSLog(@"webView:%@",NSStringFromCGSize(fittingSize));
  9. self.height = fittingSize.height;
  10. self.webView.frame = CGRectMake(0, 0, fittingSize.width, fittingSize.height);
  11. // 用通知发送加载完成后的高度
  12. [[NSNotificationCenter defaultCenter] postNotificationName:@"WEBVIEW_HEIGHT" object:self userInfo:nil];
  13. }

viewController.m中实现接收通知高度:

  1. - (void)viewDidLoad
  2. {
  3. [super viewDidLoad];
  4. self.heightDic = [[NSMutableDictionary alloc] init];
  5. // 注册加载完成高度的通知
  6. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(noti:) name:@"WEBVIEW_HEIGHT" object:nil];
  7. _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height) style:UITableViewStyleGrouped];
  8. _tableView.delegate = self;
  9. _tableView.dataSource = self;
  10. [self.tableView registerClass:[TableViewCell class] forCellReuseIdentifier:@"cell"];
  11. [self.view addSubview:_tableView];
  12. }

3.通知方法实现cell高度的赋值

  1. #pragma mark - 通知
  2. - (void)noti:(NSNotification *)sender
  3. {
  4. TableViewCell *cell = [sender object];
  5. NSLog(@"%@",@(cell.tag));
  6. if (![self.heightDic objectForKey:[NSString stringWithFormat:@"%ld",cell.tag]]||[[self.heightDic objectForKey:[NSString stringWithFormat:@"%ld",cell.tag]] floatValue] != cell.height)
  7. {
  8. [self.heightDic setObject:[NSNumber numberWithFloat:cell.height] forKey:[NSString stringWithFormat:@"%ld",cell.tag]];
  9. [self.tableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:cell.tag inSection:0]] withRowAnimation:UITableViewRowAnimationNone];
  10. }
  11. }

4.确定cell的高度

  1. -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
  2. {
  3. return [[self.heightDic objectForKey:[NSString stringWithFormat:@"%ld",indexPath.row]] floatValue];;
  4. }

5.完整项目地址

https://github.com/jijing2013/Demo

发表评论

表情:
评论列表 (有 0 条评论,233人围观)

还没有评论,来说两句吧...

相关阅读

    相关 UIWebView与WKWebView

    UIWebView UIWebView是苹果继承于UIView封装的一个加载web内容的类,它可以加载任何远端的web数据展示在你的页面上,你可以像浏览器一样前进后退刷新等操