我在UITableView中显示一篇文章.为了正确显示,我需要给代理一个单元格的高度,我想要与UIWebView的高度相同,所以我可以在WebView上禁用滚动,并将Web内容整体显示为一个静态单元格.
我的第一种方法是将其渲染在heightForRowAtIndexpathmethod中,但这显然不能正常工作,因为我需要等待UIWebViewDelegate告诉我,当Web视图完全加载并且具有高度时.过了一段时间,我找到了一个工作的解决方案,使用Web视图委托来刷新网页视图的单元格高度.
工作正常,直到屏幕尺寸变化.从旋转或全屏筛选我的UISplitView.我在didRotateFromInterfaceOrientation(fromInterfaceOrientation:UIInterfaceOrientation)中强制更新它,但这会使其闪烁约10次,然后才能进入正确的高度.我记录了这个更改,似乎WebView正在调用自己多次,导致循环.
从this日志中可以看出,从我旋转屏幕开始.
它每次重新加载时会闪烁一次,如您所见,它会自动重新加载一次.
所以.我需要一种方式来显示整个网页视图内容的内容,并在屏幕尺寸变化时可靠地获取高度.如果任何人以前以任何方式进行过管理,请告诉我.任何可以解决这个问题的人,我会给予赏金和我的长子,因为它让我疯狂.
这是我的相关代码.
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) }
解决方法
原文地址:https://www.jb51.cc/iOS/329211.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。