Ios UIWebView动态高度的UITableViewCell
我有一个表格视图,其中的单元格有一个网络视图,我希望单元格的高度与网络视图的高度相匹配 这是我使用的代码:Ios UIWebView动态高度的UITableViewCell,ios,xcode,swift,uitableview,uiwebview,Ios,Xcode,Swift,Uitableview,Uiwebview,我有一个表格视图,其中的单元格有一个网络视图,我希望单元格的高度与网络视图的高度相匹配 这是我使用的代码: func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCellWithIdentifier("newsCell") as! NewsTableVi
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("newsCell") as! NewsTableViewCell
cell.webView.loadHTMLString("test<br>test<br>test<br>test<br>test<br>test", baseURL: nil)
cell.webView.delegate = self
var webFrame = cell.webView.frame
var cellFrame = cell.frame
cell.frame = CGRectMake(cellFrame.origin.x, cellFrame.origin.y, cellFrame.width, webFrame.height + 20)
cell.backgroundColor = UIColor.redColor()
return cell
}
func webViewDidFinishLoad(webView: UIWebView) {
println("finished loading")
var frame = webView.frame
frame.size.height = 1
webView.frame = frame
var fittingSize = webView.sizeThatFits(CGSizeZero)
frame.size = fittingSize
webView.frame = frame
var height: CGFloat = frame.height
println(height)
webView.frame = CGRectMake(frame.origin.x, frame.origin.x, frame.size.width, frame.size.height)
newsTable.beginUpdates()
newsTable.endUpdates()
}
func tableView(tableView:UITableView,cellForRowAtIndexPath:nsindepath)->UITableView单元格{
将cell=tableView.dequeueReusableCellWithIdentifier(“newsCell”)设为!NewsTableViewCell
cell.webView.loadHTMLString(“test
test
test
test
test
test”,baseURL:nil)
cell.webView.delegate=self
var webFrame=cell.webView.frame
var cellFrame=cell.frame
cell.frame=CGRectMake(cellFrame.origin.x、cellFrame.origin.y、cellFrame.width、webFrame.height+20)
cell.backgroundColor=UIColor.redColor()
返回单元
}
func webViewDidFinishLoad(webView:UIWebView){
println(“已完成加载”)
var frame=webView.frame
frame.size.height=1
webView.frame=frame
var fittingSize=webView.sizeThatFits(CGSizeZero)
frame.size=配件尺寸
webView.frame=frame
变量高度:CGFloat=frame.height
println(高度)
webView.frame=CGRectMake(frame.origin.x,frame.origin.x,frame.size.width,frame.size.height)
newsTable.beginUpdates()
newsTable.endUpdates()
}
这就是结果:
webView的高度正确,但单元格的高度不正确,如何解决此问题?实现UITableview委托
HeightForRowatineXpath返回webview框架高度。TableView将调整单元格本身的大小,您只需实现
TableView(TableView:UITableView,HeightForRowatineXpath indexPath:NSIndexPath)->CGFloat
委托方法
是的,最初您不知道WebView的高度,但可以计算它,然后让TableView重新加载单元格。大概是这样的:
class TableViewController: UITableViewController, UIWebViewDelegate
{
var content : [String] = ["test1<br>test1<br>test1<br>test1<br>test1<br>test1", "test22<br>test22<br>test22<br>test22<br>test22<br>test22"]
var contentHeights : [CGFloat] = [0.0, 0.0]
// ...
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCellWithIdentifier("newsCell", forIndexPath: indexPath) as! NewsTableViewCell
let htmlString = content[indexPath.row]
let htmlHeight = contentHeights[indexPath.row]
cell.webView.tag = indexPath.row
cell.webView.delegate = self
cell.webView.loadHTMLString(htmlString, baseURL: nil)
cell.webView.frame = CGRectMake(0, 0, cell.frame.size.width, htmlHeight)
return cell
}
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat
{
return contentHeights[indexPath.row]
}
func webViewDidFinishLoad(webView: UIWebView)
{
if (contentHeights[webView.tag] != 0.0)
{
// we already know height, no need to reload cell
return
}
contentHeights[webView.tag] = webView.scrollView.contentSize.height
tableView.reloadRowsAtIndexPaths([NSIndexPath(forRow: webView.tag, inSection: 0)], withRowAnimation: .Automatic)
}
// ...
}
类TableViewController:UITableViewController、UIWebViewDelegate
{
变量内容:[字符串]=[“test1
test1
test1
test1
test1
test1”、“test22
test22
test22
test22
test22
test22”]
var contentHeights:[CGFloat]=[0.0,0.0]
// ...
重写func tableView(tableView:UITableView,cellForRowAtIndexPath:nsindepath)->UITableView单元格
{
让cell=tableView.dequeueReusableCellWithIdentifier(“newsCell”,forIndexPath:indexPath)作为!NewsTableViewCell
设htmlString=content[indexPath.row]
设htmlHeight=contentHeights[indexPath.row]
cell.webView.tag=indexPath.row
cell.webView.delegate=self
cell.webView.loadHTMLString(htmlString,baseURL:nil)
cell.webView.frame=CGRectMake(0,0,cell.frame.size.width,htmlHeight)
返回单元
}
重写func tableView(tableView:UITableView,heightForRowAtIndexPath:nsindepath)->CGFloat
{
返回contentHeights[indexPath.row]
}
func webViewDidFinishLoad(webView:UIWebView)
{
if(contentHeights[webView.tag]!=0.0)
{
//我们已经知道高度了,不需要重新装电池
返回
}
contentHeights[webView.tag]=webView.scrollView.contentSize.Heights
tableView.reloadrowsatindexpath([nsindepath(forRow:webView.tag,第0节)],withRowAnimation:.Automatic)
}
// ...
}
这对我来说很有用,用dynamic在WebView上加载Html字符串很简单
首先在TableViewCell中使用滚动禁用功能获取textView,然后使用WebView,在ContentView的所有4个面上应用零约束。现在为textview和WebView提供相同的高度约束。现在为这两个视图提供相同的文本,如下代码所示
对于干净的UI,从XIB或故事板隐藏文本视图
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 10
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
return 100
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "tableviewcell") as! tableviewcell
let theString : String = "</p><img src='https://pbs.twimg.com/profile_images/655066410087940096/QSUlrrlm.png' width=100 height=100 /><h2>Subheader</h2>"
let theAttributedString = try! NSAttributedString(data: theString.data(using: String.Encoding.utf8, allowLossyConversion: false)!,options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType], documentAttributes: nil)
cell.textview.attributedText = theAttributedString
cell.webview.loadHTMLString(theString, baseURL: nil)
return cell
}
func tableView(tableView:UITableView,numberofrowsinssection:Int)->Int{
返回10
}
func tableView(tableView:UITableView,heightForRowAt indexath:indexPath)->CGFloat{
返回UITableViewAutomaticDimension
}
func tableView(tableView:UITableView,estimatedHeightForRowAt indexath:indexPath)->CGFloat{
返回100
}
func tableView(tableView:UITableView,cellForRowAt indexath:indexPath)->UITableViewCell{
将cell=tableView.dequeueReusableCell(标识符为“tableviewcell”)设为!tableviewcell
让字符串:String=“子标题”
让theAttributedString=try!NSAttributedString(数据:theString.data(使用:String.Encoding.utf8,allowLossyConversion:false)!,选项:[NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType],DocumentAttribute:nil)
cell.textview.attributedText=指定的字符串
cell.webview.loadHTMLString(字符串,baseURL:nil)
返回单元
}
在计算webview高度后使用DispatchQueue重新加载tableViewcell高度DispatchQueue.main.async
。它避免了在重新加载webview框架时出现异常空格和崩溃
func webViewDidFinishLoad(_ webView: UIWebView)
{
var frame : CGRect = webVwModule.frame;
frame.size.height = 1;
self.webVwModule.frame.size = webVwModule.sizeThatFits(.zero)
frame.size = self.webVwModule.frame.size;
webView.frame = frame;
webViewContentHeight = self.webVwModule.frame.size.height;
isWebViewLoaded = true
DispatchQueue.main.async(execute: { () -> Void in
self.tableView.beginUpdates()
self.tableView.endUpdates()
})
}
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return webViewContentHeight!
}
Swift 4 ifau针对Swift 4修改了答案,使用
UITableView
而不是UITableViewController
在情节提要中,使用UITableView
。向其中添加1个原型单元。将单元格的reuseIdentifier保持为“单元格
”。将webView
拖动到单元格中,并将前导、尾随、顶部和底部约束设置为0。将单元格类设置为TableViewCell
TableViewCell.swift
将webView插座连接到情节提要:
import UIKit
class TableViewCell: UITableViewCell {
@IBOutlet weak var webView: UIWebView!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
import UIKit
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UIWebViewDelegate {
@IBOutlet weak var tableView: UITableView!
var content : [String] = ["test1<br>test1<br>test1", "test22<br>test22<br>test22<br>test22<br>test22<br>test22"]
var contentHeights : [CGFloat] = [0.0, 0.0]
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
tableView.dataSource = self
tableView.delegate = self
}
public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
return content.count
}
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
let cell : TableViewCell = tableView.dequeueReusableCell(withIdentifier: "cell") as! TableViewCell
let htmlString = content[indexPath.row]
let htmlHeight = contentHeights[indexPath.row]
cell.webView.tag = indexPath.row
cell.webView.delegate = self
cell.webView.loadHTMLString(htmlString, baseURL: nil)
cell.webView.frame = CGRect(x: 0, y: 0, width: cell.frame.size.width, height: htmlHeight)
return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if contentHeights[indexPath.row] != 0 {
return contentHeights[indexPath.row]
}
}
public func webViewDidFinishLoad(_ webView: UIWebView){
if (contentHeights[webView.tag] != 0.0)
{
// we already know height, no need to reload cell
return
}
contentHeights[webView.tag] = webView.scrollView.contentSize.height
tableView.reloadRows(at: [IndexPath(row: webView.tag, section: 0)], with: .automatic)
}
}
ViewController.swift
将tableView插座连接到情节提要:
import UIKit
class TableViewCell: UITableViewCell {
@IBOutlet weak var webView: UIWebView!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
import UIKit
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UIWebViewDelegate {
@IBOutlet weak var tableView: UITableView!
var content : [String] = ["test1<br>test1<br>test1", "test22<br>test22<br>test22<br>test22<br>test22<br>test22"]
var contentHeights : [CGFloat] = [0.0, 0.0]
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
tableView.dataSource = self
tableView.delegate = self
}
public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
return content.count
}
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
let cell : TableViewCell = tableView.dequeueReusableCell(withIdentifier: "cell") as! TableViewCell
let htmlString = content[indexPath.row]
let htmlHeight = contentHeights[indexPath.row]
cell.webView.tag = indexPath.row
cell.webView.delegate = self
cell.webView.loadHTMLString(htmlString, baseURL: nil)
cell.webView.frame = CGRect(x: 0, y: 0, width: cell.frame.size.width, height: htmlHeight)
return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if contentHeights[indexPath.row] != 0 {
return contentHeights[indexPath.row]
}
}
public func webViewDidFinishLoad(_ webView: UIWebView){
if (contentHeights[webView.tag] != 0.0)
{
// we already know height, no need to reload cell
return
}
contentHeights[webView.tag] = webView.scrollView.contentSize.height
tableView.reloadRows(at: [IndexPath(row: webView.tag, section: 0)], with: .automatic)
}
}
导入UIKit
类ViewController:UIViewController、UITableViewDelegate、UITableViewDataSource、UIWebViewDelegate{
@IBVAR表格视图:UITableView!
变量内容:[字符串]=[“test1
test1
test1”、“test22
test22
test22
test22
test22
test22”]
var contentHeights:[CGFloat]=[0.0,0.0]
重写func viewDidLoad(){
super.viewDidLoad()
//典型情况下,在加载视图后执行任何其他设置