func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if let cell = tableView.cellForRow(at: indexPath) as? ExpandablePlaneTableViewCell {
return 400.0
}
return 75.0
}
我想更改单元格的大小,但在 heightForRowAt
内部它看不到我的 cell
并崩溃了。当我把 if let
放在那里时,检查它没有进入 block 内部,只需要 75。
谁能告诉我问题出在哪里?这对我来说太奇怪了! 我已经将委托(delegate)设置为 self 。因此它调用了该函数,但无法在那里检测到我的手机。
更新
在我的 ExpandablePlaneTableViewCell
中,我有一个变量:
var exapanded = false
稍后在我的 ViewController 中:单击单元格中的按钮,我运行我的委托(delegate)方法:
func expandViewButtonTapped(_ sender: ExpandablePlaneTableViewCell, for indexPath: IndexPath) {
sender.exapanded = true
tableView.reloadRows(at: [indexPath], with: .automatic)
}
在我想展开它并重新加载单元格之后
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "expandableCell", for: indexPath) as! ExpandablePlaneTableViewCell
cell.delegate = self
cell.indexPath = indexPath
return cell
}
最佳答案
不要尝试获取 heightForRowAt
中的单元格。在您的情况下当然没有理由这样做。
您似乎希望某些类型的单元格的高度是一个值,而其他类型的高度是另一个值。
只需使用与 cellForRowAt
相同的基本逻辑,基于 indexPath
,即可确定要返回的高度。 换句话说,根据您的数据模型而不是单元格做出决定。
关于ios - 为什么在 heightForRowAt 方法中我的单元格为零?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48192847/