[英]Random scrolling while reloading uitableviewcell, iOs/Swift
I am developing news feed and I am using uitableview to display data. 我正在开发新闻提要,并且正在使用uitableview显示数据。 I am loading each cell data synchronically in other thread and use protocol method to display loaded data:
我正在其他线程中同步加载每个单元格数据,并使用协议方法显示加载的数据:
func nodeLoaded(node: NSMutableDictionary) {
for var i = 0; i < nodesArray.count; ++i {
if ((nodesArray[i]["id"] as! Int) == (node["id"] as! Int)) {
nodesArray[i] = node
CATransaction.setDisableActions(true)
self.tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.None)
CATransaction.setDisableActions(false)
}
}
}
The problem is that when I scrolling down (while tableview updating), tableview push me on the top. 问题是当我向下滚动(更新表视图时)时,表视图将我推到顶部。 I was searching answer on this problem, but nothing helps me.
我正在寻找有关此问题的答案,但没有任何帮助。 I already tried to disable animation:
我已经尝试禁用动画:
CATransaction.setDisableActions(true)
self.tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.None)
CATransaction.setDisableActions(false)
2) 2)
UIView.setAnimationsEnabled(false)
self.tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.None)
UIView.setAnimationsEnabled(true)
But the story always the same. 但是故事总是一样的。 I have the desired effect when I simply don't use
self.tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.None)
, but in this case data reload only if I scroll down and then return back to cell. 当我根本不使用
self.tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.None)
,就可以达到预期的效果,但是在这种情况下,只有当我向下滚动然后返回到单元格时,数据才会重新加载。
Maybe the problem is that I use auto layout and tableview recalculate its height every time? 也许问题是我使用自动布局,并且tableview每次都重新计算其高度? But I don't know how to fix it
但我不知道如何解决
You can build the reload mechanism yourself. 您可以自己构建重载机制。
Iterate over all visible cells ( tableView.visibleCells
) and apply the same logic to them as you would in -tableView:cellForRowAtIndexPath:
. 遍历所有可见的单元格(
tableView.visibleCells
)并对其应用与-tableView:cellForRowAtIndexPath:
相同的逻辑。
If you need the index path to perform this update, you can ask the table view for it ( -indexPathForCell:
). 如果您需要索引路径来执行此更新,则可以向表视图询问(
-indexPathForCell:
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.