3

I tried to add a refresh control on my table view but I couldn't make it as I wanted.

I have a view that contains, among other things, a table view. My ViewController's viewDidLoad function is the following:

override func viewDidLoad() {
    super.viewDidLoad()

    // set the refresh control
    self.refreshControl = UIRefreshControl()
    self.refreshControl.attributedTitle = NSAttributedString(string: "Pull to refresh")
    self.refreshControl.addTarget(self, action: "getData", forControlEvents: UIControlEvents.ValueChanged)
    self.tableView.addSubview(refreshControl)
}

Everything works except that when I pull down the rows to triggers the refresh control, the refresh control appears BEHIND the first row and ends up on top of the first row (normal, I'm pulling those rows down...).

What I would like to get is like in many apps, when you pull down your rows, the refresh control acts like if it was the row #-1 and unhide from the top as soon as we are pulling down the first row.

Is that possible with the built-in refresh control?

Thanks

Top-Master
  • 7,611
  • 5
  • 39
  • 71
Nico
  • 6,269
  • 9
  • 45
  • 85

1 Answers1

7

Well I found part of the answer with this

It doesn't solve my problem but at least it's better... now the "Pull to refresh" doesn't appear through the first row as soon as I pull... So in Swift you can either do this:

Replace :

self.refreshControl = UIRefreshControl()
self.refreshControl.attributedTitle = NSAttributedString(string: "Pull to refresh")
self.refreshControl.addTarget(self, action: "getData", forControlEvents: UIControlEvents.ValueChanged)
self.tableView.addSubview(refreshControl)

By :

let tableViewController = UITableViewController()
tableViewController.tableView = self.tableView
            
self.refreshControl = UIRefreshControl()
self.refreshControl.attributedTitle = NSAttributedString(string: "Pull to refresh")
self.refreshControl.addTarget(self, action: "getData", forControlEvents: UIControlEvents.ValueChanged)
tableViewController.refreshControl = self.refreshControl

Or just add this to the first part instead of replacing it by the second part.

self.tableView.insertSubview(refreshControl, atIndex: 0)

Both seem to work, and I don't really know if one is better practice than the other one, but one is a direct setter while the latter is like hopping UIRefreshControl does set-up itself automatically.

Top-Master
  • 7,611
  • 5
  • 39
  • 71
Nico
  • 6,269
  • 9
  • 45
  • 85