ios – UIWebView的整个内容嵌入在UITableView中

前端之家收集整理的这篇文章主要介绍了ios – UIWebView的整个内容嵌入在UITableView中前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我一直在努力这两天,所以我来到互联网的智慧人手中.

我在UITableView中显示一篇文章.为了正确显示,我需要给代理一个单元格的高度,我想要与UIWebView的高度相同,所以我可以在WebView上禁用滚动,并将Web内容整体显示为一个静态单元格.

我的第一种方法是将其渲染在heightForRowAtIndexpathmethod中,但这显然不能正常工作,因为我需要等待UIWebViewDelegate告诉我,当Web视图完全加载并且具有高度时.过了一段时间,我找到了一个工作的解决方案,使用Web视图委托来刷新网页视图的单元格高度.

工作正常,直到屏幕尺寸变化.从旋转或全屏筛选我的UISplitView.我在didRotateFromInterfaceOrientation(fromInterfaceOrientation:UIInterfaceOrientation)中强制更新它,但这会使其闪烁约10次,然后才能进入正确的高度.我记录了这个更改,似乎WebView正在调用自己多次,导致循环.

this日志中可以看出,从我旋转屏幕开始.

它每次重新加载时会闪烁一次,如您所见,它会自动重新加载一次.

所以.我需要一种方式来显示整个网页视图内容内容,并在屏幕尺寸变化时可靠地获取高度.如果任何人以前以任何方式进行过管理,请告诉我.任何可以解决这个问题的人,我会给予赏金和我的长子,因为它让我疯狂.

这是我的相关代码.

  1. func tableView(tableView: UITableView,heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
  2. switch indexPath.row {
  3. case 4:
  4. //Content
  5. print("Height for row called")
  6. return CGFloat(webViewHeight)
  7. }
  8.  
  9. func tableView(tableView: UITableView,cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
  10. switch (indexPath.row){
  11. //HTML Content View
  12. case 4:
  13. let cell = tableView.dequeueReusableCellWithIdentifier("ContentCell",forIndexPath: indexPath)
  14. var contentCell = cell as? ContentCell
  15. if contentCell == nil {
  16. contentCell = ContentCell()
  17. }
  18. contentCell?.contentWebView.delegate = self
  19. contentCell?.contentWebView.scrollView.userInteractionEnabled = false
  20. contentCell?.contentWebView.loadHTMLString((post?.contentHTML)!,baseURL: nil)
  21. print("Cell For row at indexpath called")
  22. return contentCell!
  23. }
  24. func webViewDidFinishLoad(webView: UIWebView) {
  25. updateHeight()
  26. }
  27.  
  28. func updateHeight(){
  29. let webView = (self.tableView.cellForRowAtIndexPath(NSIndexPath(forRow: 4,inSection: 0)) as! ContentCell).contentWebView
  30. if self.webViewHeight != Double(webView.scrollView.contentSize.height) {
  31. print("PrevIoUs WV Height = \(self.webViewHeight),New WV Height = \(webView.scrollView.contentSize.height)")
  32. self.webViewHeight = Double(webView.scrollView.contentSize.height)
  33. tableView.reloadRowsAtIndexPaths([NSIndexPath(forRow: 4,inSection: 0)],withRowAnimation: .Automatic)
  34. } else {
  35. return
  36. }
  37. }
  38.  
  39. override func didRotateFromInterfaceOrientation(fromInterfaceOrientation: UIInterfaceOrientation) {
  40. print("rotated")
  41. self.updateHeight()
  42. //tableView.reloadRowsAtIndexPaths([NSIndexPath(forRow: 4,withRowAnimation: .Automatic)
  43. }

解决方法

我建议您独立于表视图计算Web视图高度,并将维度存储为数据本身的一部分,并使用其在heightForRowAtIndexPath调用中返回.它更容易,因为您不必处理在表视图显示期间计算表高度.当html内容未加载时,使用标准高度和Web视图的消息.

猜你在找的iOS相关文章