更改单元格高度后,UITableView 在滚动时跳动

Kyl*_*uth 5 iphone uitableview uiscrollview ios swift

所以我有一个 tableView,我想在点击时更改单元格的高度。嗯,实际上,我正在用更大的电池替换它。点击后,我调用:

tableView.beginUpdates()
tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
tableView.endUpdate()
Run Code Online (Sandbox Code Playgroud)

然后我修改我的cellForRowAtIndexPath以返回正确的新单元格和高度。sizeThatFits通过在单元格的实现中覆盖来自动计算单元格的高度:

override func sizeThatFits(size: CGSize) -> CGSize {
    return CGSizeMake(size.width, myHeight)
}
Run Code Online (Sandbox Code Playgroud)

奇怪的是,在我这样做之后,向下滚动没问题,但是当我向上滚动时,表格每秒跳跃 5 个像素左右,直到到达顶部。当我到达桌子顶部后,问题就消失了,并且两个方向都没有跳跃。知道为什么会发生这种情况吗?我想这与新的单元格高度取代其他单元格有关,但我不明白为什么 tableView 不处理这个问题。任何帮助表示赞赏!

谢谢!

编辑:从 cellForRowAtIndexPath 添加代码:

 if self.openedCellIndex != nil && self.openedCellIndex == indexPath {
     cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as ListCell
     (cell as ListCell).updateWithDetailView(dayViewController!.view)
 } else {
     cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as ListCell
     (cell as ListCell).updateWithData(eventDay: store.events![indexPath.row], reminderDay: store.reminders![indexPath.row])
 }
 return cell
Run Code Online (Sandbox Code Playgroud)

The*_*eSD 0

您应该包含 cellForRow 和 heightForRow 的代码,但我会对其进行盲测。

当在 cellForRow 中点击单元格时,您应该存储该单元格的索引,然后重新加载数据或仅加载该单元格。然后在heightForRow中使用if(yourTappedCell){return preferredHeight;}