Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/110.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/vim/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 我如何实现这样一种方法:当调用时,表视图单元格';身高变化?_Ios_Swift_Uitableview - Fatal编程技术网

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上运行了你的代码,同样的错误也发生了!知道为什么吗?