UIRefreshControl glitching in combination with custom TableViewCell

試著忘記壹切 提交于 2020-01-03 03:04:39

问题


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?


回答1:


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.




回答2:


This is how I go about adding a refresh control in my UITableView with custom cells.

lazy var refreshControl: UIRefreshControl = {

    let refreshControl = UIRefreshControl()
    refreshControl.addTarget(self, action: #selector(self.refresh(_:)), for: UIControlEvents.valueChanged)
    return refreshControl

}()

func refresh(_ refreshControl: UIRefreshControl) {

    self.tableView.reloadData()
}

override func viewDidLoad() {
    super.viewDidLoad()
    self.tableView.addSubview(refreshControl)
}

Possibly add endRefreshing when you're done with a task, and not in the action of the refreshControl itself.



来源:https://stackoverflow.com/questions/46760383/uirefreshcontrol-glitching-in-combination-with-custom-tableviewcell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!