问题
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