为什么在高度为RowAt 方法中,我的单元格为零


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检查它不会进入块内部,只需要 75。

谁能告诉我问题是什么?这对我来说太奇怪了!我已经将委托设置为自我。所以它调用函数,但无法检测到我的单元格。

更新

在我的ExpandablePlaneTableViewCell中,我有一个变量:

var exapanded = false

稍后在我的视图控制器中:单击单元格中的按钮,我运行委托方法:

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,来确定要返回的高度。换句话说,决策基于数据模型,而不是单元格。

相关内容

最新更新