重新加载可扩展表视图单元时如何避免闪烁?

问题描述 投票:0回答:2

我需要扩展UITableView的细胞,但是这样做时我会看到一个闪烁。

我有这个实现:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    // Switch row state
    expandedFlags[indexPath.row] = !expandedFlags[indexPath.row]

    UIView.animate(withDuration: 0.5, delay: 0, options: .curveEaseOut, animations: { () -> Void in
            tableView.beginUpdates()
            tableView.reloadRows(at: [IndexPath(row: indexPath.row, section: 0)], with: UITableViewRowAnimation.automatic)
            tableView.endUpdates()
        }, completion: nil)
}

有没有办法避免重新加载细胞时轻弹?我已阅读了几篇帖子,但对我的方案不起作用。

ios uitableview expand flicker
2个回答
0
投票
tableView.beginUpdates()
....
tableView.endUpdates()

已经有自己的动画UIView动画可能会使闪烁


0
投票

嗨花了这么多时间,我终于找到了解决方案,以便在扩展或折叠tableview单元格时在表格视图中停止此闪烁问题。

在第一步中,您必须获取tableview的当前偏移量,然后停止uiview动画并告诉您的tableview您将更新tableview中的某些行。然后传递你当前的索引[IndexPath(row:0,section:3)],就像这是我的。你的tableview就像魅力一样。

let currentOffset = self.tableView.contentOffset
UIView.setAnimationsEnabled(false)
self.tableView.beginUpdates()
self.tableView.reloadRows(at: [IndexPath(row: 0, section: 3)], with: .none)
tableView.endUpdates()
UIView.setAnimationsEnabled(true)
self.tableView.setContentOffset(currentOffset, animated: false)
© www.soinside.com 2019 - 2024. All rights reserved.