Ios 我如何实现这样一种方法:当调用时,表视图单元格';身高变化?
我想实现一个如下所示的方法:Ios 我如何实现这样一种方法:当调用时,表视图单元格';身高变化?,ios,swift,uitableview,Ios,Swift,Uitableview,我想实现一个如下所示的方法: setCellHeightForIndexPath(someIndexPath, 80) tableView.beginUpdates() tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.None) tableView.endUpdates() 然后该索引路径处的表视图单元格的高度会突然变为80 我之所以要这样做,是因为我希望单元格的高
setCellHeightForIndexPath(someIndexPath, 80)
tableView.beginUpdates()
tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.None)
tableView.endUpdates()
然后该索引路径处的表视图单元格的高度会突然变为80
我之所以要这样做,是因为我希望单元格的高度设置为加载完HTML后web视图内容的高度。由于我只能在web视图完成加载后获取其内容大小,因此我不能立即设置单元格高度
有关更多信息,请参见此
因此,在webviewdiffinishload
方法中,我只需获取web视图的内容高度,将web视图的高度设置为该高度,然后调用此方法来设置单元格的高度
似乎只有在调用heightforrowatinexpath
时,单元格高度才能更改。我认为该方法将使用与之类似的方法。我想我需要储存一系列的高度也许?我就是想不出一个方法来实现这一点
我如何实现这种方法
注意:不要告诉我这是不可能的。在Instagram应用程序中,我可以看到不同高度的图像完全适合表格视图单元格。这些图像与我的网络视图相似。它们都需要时间来加载
编辑:
让我展示一下我在这方面的一些尝试:
var results: [Entry] = []
var cellHeights: [CGFloat] = []
var webViews: [UIWebView] = []
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return results.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("resultCell")
let webView = cell!.contentView.viewWithTag(1) as! UIWebView
webView.loadHTMLString(results[indexPath.row].htmlDescriptionForSearchMode(.TitleOnly), baseURL: nil)
webView.delegate = self
webView.scrollView.scrollEnabled = false
webViews.append(webView)
cellHeights.append(400)
webView.stringByEvaluatingJavaScriptFromString("highlightSearch(\"day\")")
return cell!
}
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return indexPath.row < cellHeights.count ? cellHeights[indexPath.row] : 400
}
func webViewDidFinishLoad(webView: UIWebView) {
let height = CGFloat(webView.stringByEvaluatingJavaScriptFromString("document.height")!.toFloat()!)
webView.frame = CGRect(origin: webView.frame.origin, size: CGSizeMake(webView.frame.width, height))
print(height)
if let index = webViews.indexesOf(webView).first {
cellHeights[index] = height
tableView.beginUpdates()
tableView.reloadRowsAtIndexPaths([NSIndexPath(forRow: index, inSection: 0)], withRowAnimation: .None)
tableView.endUpdates()
}
}
您可以这样实现 取一个全局CGFloat表示高度,另一个表示索引 现在,当您需要更改高度时,请同时设置值并使用
[self.yourTableview beginUpdate]
[self.yourTableview endUpdate]
将更新您的手机
在cellforrowatinexpath
中,您应该使用dequeueReusableCellWithIdentifier:(NSString*)identifier-forIndexPath:(NSIndexPath*)
确保每次都更新单元格
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath == yourIndexpath) {
return gloablVariable;
} else {
return defauleHeight
}
}
希望有帮助您不能将新单元格高度“推”到表视图上。相反,您需要将表视图从高度“拉”到新高度,以便在索引路径中显示,并准备好提供新高度
当行r
的单元加载完成时,您需要更新模型,使其知道行r
的新高度。之后,您需要告诉表视图重新加载自身,如下所示:
setCellHeightForIndexPath(someIndexPath, 80)
tableView.beginUpdates()
tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.None)
tableView.endUpdates()
这将启动更新单元格的过程<将调用code>heightforrowatinexpath
。您的代码将返回新高度。之后将调用cellforrowatinexpath
。您的代码应该准备好返回已完成加载的单元格,而不启动新的数据加载。我尝试使用自动布局和自动单元格高度计算来实现它
也许这有助于为您指明正确的方向:
只是一段摘录:
func webViewDidFinishLoad(webView: UIWebView) {
if webView.loading {
return
}
guard let cell = webView.superview?.superview as? WebViewCell else {
print("could not get cell")
return
}
guard let index = websites.map({$0.cell}).indexOf(cell) else {
print("could not get index")
return
}
// get website
let website = websites[index]
print("finished loading \(website.urlString)...")
// get contentheight from webview
guard let heightString = webView.stringByEvaluatingJavaScriptFromString("document.height") else {
print("could not get heightString")
return
}
guard let contentHeight = Float(heightString) else {
print("could not convert heightString")
return
}
cell.webViewHeightConstraint.constant = CGFloat(contentHeight)
tableView.beginUpdates()
tableView.endUpdates()
}
您是否实现了索引路径中的
高度?还是通过设置约束来使用自动单元格高度计算?我以前尝试过使用带约束的自动单元格高度,就像我在上一个问题中所说的,它不起作用,表格单元格保持在44@AndréSlotta的高度(我想)我照你说的做了。但是,当我运行应用程序时,应该显示很长内容的web视图消失了。控制台显示:WebKit在webView:didFinishLoadForFrame:delegate:中丢弃了一个未捕获的异常:尝试从第0节中删除第7行,该节在更新之前仅包含4行在您的表视图之外。这是不正确的,因为对不同单元格调用cellforrowatinexpath
的顺序基本上是随机的。您可以使用web视图本身的标记
属性将其行存储在表中。在cellforrowatinexpath
中设置webView.tag=indexPath.row
。现在代替webView.indexesOf(webView)。首先
您可以在webView.didfishload
中编写webView.tag
。好的,我会试试。现在我的web视图似乎出了问题。实际上,他们的顺序在不断变化。例如,第一个单元格显示第二个单元格应显示的内容,时间仅为几分之一秒。然后,它会变回其原始内容,但在变回第二个单元格的内容之前只有一小部分时间,但高度似乎正常。@Sweeper我知道发生了什么-您已经使用了viewWithTag
,因此使用tag将不起作用(您的代码cell!.contentView.viewWithTag(1)as!UIWebView
将停止工作)。你能为你的单元格创建一个自定义类并为UIWebView
添加IBOutlet
,这样你就不必使用tag
在单元格上搜索子视图了吗?噢!我负责你的项目,这正是我想要的!让我检查一下你的代码,研究一下你是如何做到的+1如果有不清楚的地方,可以自由提问。很高兴我能帮上忙:)所以我把你的网站
重命名为结果
,WebViewCell
重命名为ResultCell
,上面说$0.cell
在映射
方法中是nil
。似乎在cellforrowatinexpath
之前调用了webViewDidFinishLoad
。为什么?这完全没有道理。听起来确实很奇怪:)你能和我分享一下你的代码吗?我找到了原因!它只适用于iPhone 6s Plus!我在iPhone 5上运行了你的代码,同样的错误也发生了!知道为什么吗?