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

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

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

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

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

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

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

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

这是我的相关代码.

@H_404_16@func tableView(tableView: UITableView,heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { switch indexPath.row { case 4: //Content print("Height for row called") return CGFloat(webViewHeight) } func tableView(tableView: UITableView,cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { switch (indexPath.row){ //HTML Content View case 4: let cell = tableView.dequeueReusableCellWithIdentifier("ContentCell",forIndexPath: indexPath) var contentCell = cell as? ContentCell if contentCell == nil { contentCell = ContentCell() } contentCell?.contentWebView.delegate = self contentCell?.contentWebView.scrollView.userInteractionEnabled = false contentCell?.contentWebView.loadHTMLString((post?.contentHTML)!,baseURL: nil) print("Cell For row at indexpath called") return contentCell! } func webViewDidFinishLoad(webView: UIWebView) { updateHeight() } func updateHeight(){ let webView = (self.tableView.cellForRowAtIndexPath(NSIndexPath(forRow: 4,inSection: 0)) as! ContentCell).contentWebView if self.webViewHeight != Double(webView.scrollView.contentSize.height) { print("PrevIoUs WV Height = \(self.webViewHeight),New WV Height = \(webView.scrollView.contentSize.height)") self.webViewHeight = Double(webView.scrollView.contentSize.height) tableView.reloadRowsAtIndexPaths([NSIndexPath(forRow: 4,inSection: 0)],withRowAnimation: .Automatic) } else { return } } override func didRotateFromInterfaceOrientation(fromInterfaceOrientation: UIInterfaceOrientation) { print("rotated") self.updateHeight() //tableView.reloadRowsAtIndexPaths([NSIndexPath(forRow: 4,withRowAnimation: .Automatic) }

解决方法

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

猜你在找的iOS相关文章