我有一个表格视图,其中包含一个webView的单元格,我希望该单元格的高度与该webView的高度相匹配。
这是我使用的代码:
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()
}
结果是这样的:https :
//postimg.cc/image/8qew1lqjj/
该webView是正确的高度,但单元格不是,如何解决此问题?
TableView会自动调整单元格的大小,您只需要实现tableView(tableView: UITableView, heightForRowAtIndexPath 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)
}
// ...
}
我正在研究具有动态单元格高度的表视图。 由于我的要求,我必须展示40种不同的cell.Each细胞是不同的。 例如。 一个单元有2个标签,一个图像视图另一个单元只有2个图像视图,另一个单元格有5个标签,1个文本视图,2个按钮,2个图像查看 我的问题是我无法静态定义高度。每个单元格中的.labels和textview都基于运行时的文本。因此,根据各个元素,我需要调整高度。所以我根据下面的编码计算了标
我有一个UIScrollView,嵌套在一个内容视图中,它有两个嵌套的子视图,一个具有已知高度的常规UIView,以及一个具有动态高度的容器视图,具体取决于内容。就像这样: 视图如下所示: 我的约束设置如下: 滚动视图被限制在其超级视图(即视图)的尾缘、前缘、顶端和下边缘 内容视图被约束到其超级视图(即滚动视图)的尾部、前导、顶部和底部边缘。它还具有与主视图(即视图)相等的宽度约束,因此滚动视图的
问题内容: 我有这个HTML结构: 我想在主体部分(#body)中包含三个部分而不会溢出。因此,我需要在中间部分使用滚动条。 我尝试了这个CSS: 和这个: 但是它们都不起作用。 我在JSFiddle上做了一个例子。 我可以仅使用CSS和HTML来做到这一点吗?我宁愿避免使用Javascript。 问题答案: Flexbox是一种现代替代方案,可让您无需固定高度或JavaScript即可执行此操作
我有一些文本内容显示在UIWebView上,这是普通的html。当前段落以黄色突出显示,用户选择了单词“如果”。(链接到图像:http://i.stack.imgur.com/GKp9h.png) 1)当用户在uiwebiew上选择一些文本时,我如何执行动态高亮?也就是说,当用户选择文本时,选择的文本会以紫色高亮? 例如,我喜欢单词“如果”用紫色突出显示(也许使用window.get选择() ?
DynamicHeights是一个动态表格元素高度(动态TableViewCell)的例子。实际应用场景在iOS开发中会经常遇到不规律的TableViewCell,往往需要根据内容的多少而动态调整这些Cell的大小。在http://www.cimgf.com/2009/09/23/uitableviewcell-dynamic-height/ 中,有详细的解说开发的流程以及这么处理的原因。里面的大
问题内容: 我正在使用一个教程使用CSS3和jQuery创建翻页卡效果,但在将高度调整为内容长度的同时仍然在中心水平位置翻转时遇到了问题。 码: 问题答案: 这是 jsFiddle 上的可行 解决方案。 JS: 定义的高度不灵活,因此您看到的就是定义的高度。由于高度将不会保持恒定,因此前部或后部需要具有定义的高度,以匹配最高的元素。在示例中,较高,因此将其更新为相同的高度,从而使您能够在中心实