UIRefreshControl 故障与自定义 TableViewCell 结合使用 [英] UIRefreshControl glitching in combination with custom TableViewCell

查看:27
本文介绍了UIRefreshControl 故障与自定义 TableViewCell 结合使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个奇怪的问题,当我将 UIRefreshControlUITableView 和自定义 UITableViewCells 结合使用时,它会出现故障.如果我使用基本的(在 Xcode 的检查器面板中设置),它就可以正常工作.请参阅

override func viewDidLoad() {super.viewDidLoad()navigationController?.navigationBar.prefersLargeTitles = truerefreshControl = UIRefreshControl()refreshControl?.addTarget(self, action: #selector(self.refresh), for: .valueChanged)tableView.refreshControl = refreshControl刷新()}@objc 函数刷新(){tableView.reloadData()refreshControl?.endRefreshing()}覆盖 func tableView(_ tableView: UITableView, numberOfRowsInSection 部分: Int) ->整数{返回 8}覆盖 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) ->UITableViewCell {let cell = tableView.dequeueReusableCell(withIdentifier: "cell")返回单元格!}

UITableView 和 UITableViewCell 上的检查器中的设置是默认设置.我在多个项目中遇到了这个问题.上面的代码在一个干净的项目中.prefersLargeTitles = false 时,刷新控件也会跳转.

如何使用自定义 TableViewCell 使刷新控件正确运行?

解决方案

我现在避免上述故障的方法是将 tableView.reloadData() 调用延迟一小段时间:

self.tableView.refreshControl?.endRefreshing()self.tableView.perform(#selector(self.tableView.reloadData), with: nil, afterDelay: 0.05)

在我看来,这不是真正的修复,而是一种黑客攻击.

I'm having a weird issue where an UIRefreshControl is glitching when I use it in combination with an UITableView and custom UITableViewCells. If I use basic ones (set in the inspector panel in Xcode) it works just fine. See GIFs on Imgur.

override func viewDidLoad() {
    super.viewDidLoad()

    navigationController?.navigationBar.prefersLargeTitles = true

    refreshControl = UIRefreshControl()
    refreshControl?.addTarget(self, action: #selector(self.refresh), for: .valueChanged)
    tableView.refreshControl = refreshControl

    refresh()
}

@objc func refresh() {
    tableView.reloadData()
    refreshControl?.endRefreshing()
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 8
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell")
    return cell!
}

Settings in inspector on UITableView and UITableViewCell are default. I'm having this issue in multiple project. The code above is in a clean project. The refresh control also jumps when prefersLargeTitles = false.

How do I get the refresh control to behave correctly with a custom TableViewCell?

解决方案

The way I avoid the above glitch nowadays is by delaying the tableView.reloadData() call for a small amount of time:

self.tableView.refreshControl?.endRefreshing()
self.tableView.perform(#selector(self.tableView.reloadData), with: nil, afterDelay: 0.05)

Not really a fix but more of a hack in my opinion.

这篇关于UIRefreshControl 故障与自定义 TableViewCell 结合使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆