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

所以我有一个tableView,我想要改变一个单元格的高度,当轻拍。 实际上,我用一个更大的单元来代替它。 点击,我打电话给:

tableView.beginUpdates() tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: .Fade) tableView.endUpdate() 

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

 override func sizeThatFits(size: CGSize) -> CGSize { return CGSizeMake(size.width, myHeight) } 

奇怪的是,这样做后,向下滚动是好的,但是当我向上滚动时,桌子每秒跳跃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 

你应该包含你的代码为cellForRow和heightForRow,但我会给它一个盲点。

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

不幸的是,这似乎没有一个简单的答案。 我已经在多个iOS应用上挣扎了。

我find的唯一的解决办法是一旦它再次出现,以编程方式滚动到你的UITableView的顶部。

 [self.tableView setContentOffset:CGPointMake(0, 0 - self.tableView.contentInset.top) animated:YES]; 

要么

 self.tableView.contentOffset = CGPointMake(0, 0 - self.tableView.contentInset.top); 

希望这可以接受的工作,而仍然能够使用dynamic单元格高度=)