更改单元格高度后,UITableView在滚动时跳动 [英] UITableView jumpy on scroll after changing cell height
问题描述
所以我有一个tableView,我想在点击时更改单元格的高度。好吧,实际上,我正用一个更大的细胞替换它。
点击,我打电话:
So I have a tableView where I want to change the height of a cell when tapped. Well, actually, I am replacing it with a bigger cell. On tap, I call:
tableView.beginUpdates()
tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
tableView.endUpdate()
然后我修改我的 cellForRowAtIndexPath
返回正确的新单元格和高度。通过在单元格的实现中覆盖 sizeThatFits
自动计算单元格的高度:
And then I modify my cellForRowAtIndexPath
to return the right new cell and height. The cell's height is being automatically calculated by overriding sizeThatFits
in the cell's implementation:
override func sizeThatFits(size: CGSize) -> CGSize {
return CGSizeMake(size.width, myHeight)
}
奇怪在我这样做之后,向下滚动很好,但是当我向上滚动时,桌子每秒跳跃5个左右的像素,直到我到达顶部。在我到达桌面顶部后,问题就消失了,任何一个方向都没有跳跃。知道为什么会这样吗?我想它与新的单元格高度取代其他单元格有关,但我不明白为什么tableView没有处理这个问题。任何帮助表示赞赏!
Oddly enough, after I do this, scrolling downwards is fine, but when I scroll upwards, the table jumps 5 or so pixels every second until I reach the top. After I reach the top of the table, the problem is gone and there is no jumping going in either direction. Any idea why this is happening? I imagine it has something to do with the new cell height displacing the other cells, but I can't see why the tableView is not taking care of this. Any help is appreciated!
谢谢!
编辑:从cellForRowAtIndexPath添加代码:
Added code from 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
推荐答案
不幸的是,似乎没有一个简单的答案。我在多个iOS应用程序上一直在努力。
Unfortunately, there does not seem to be a simple answer to this. I have struggled with it on multiple iOS apps.
我找到的唯一解决方案是再次出现时以编程方式滚动到UITableView的顶部。
The only solution I have found is to programmatically scroll to the top of your UITableView once it appears again.
[self.tableView setContentOffset:CGPointMake(0, 0 - self.tableView.contentInset.top) animated:YES];
OR
self.tableView.contentOffset = CGPointMake(0, 0 - self.tableView.contentInset.top);
希望这是一个可接受的解决方案,同时仍能使用动态单元格高度=)
Hope this an acceptable work around while still being able to use dynamic cell heights =)
这篇关于更改单元格高度后,UITableView在滚动时跳动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!