Search code examples
swiftxcodeuitableviewuirefreshcontrol

UIRefreshControl glitching in combination with custom TableViewCell


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.

Normal behaving refresh control with cell style set to basic Glitching refresh control with cell style set to custom, containing a simple UILabel

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?


Solution

  • 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.