Search code examples
iosswiftuitableviewpull-to-refresh

RefreshControl bugs first Cell of TableView, after using Back on NavBar


I have a TableView embedded in NavigationController (and that's embedded in a TabBar Controller). I have a username button in each cell that segues to the user's profile.

enter image description here

However after I hit 'back' on the profile page's nav bar, and gets directed back to tableview, it seems like I bugged something.

enter image description here

enter image description here

And if I pull down again, it refreshs but this time, top row's username becomes unclickable while other rows work as expected.

Please note that I call self.theRefreshControl = UIRefreshControl() in viewDidLoad in tableView but I am not sure if it is or isn't getting called, after using Back button.

Please note that I perform segue with prepareForSegue.


To wrap up

  • If I go to the notifications page and try clicking username, username button click and segue works

  • If I go to the notifications, and pull the refresh, username button in first row doesn't work

  • If I go to the notifications and don't do any refresh, the username buttons segues and back works (so it doesn't bug).

  • If I go to the notifications and pull to refresh, the refreshControl icon appears and disappears, but now, it doesn't let me click on the username button on the top row.

  • If I do refresh on notifications, it doesn't let me click on the username in the top row. If I click on the second username in the second cell, it segues to user's profile, but when I click back button, then it bugs like this:

enter image description here

Code in ViewDidLoad():

 override func viewDidLoad() {
  super.viewDidLoad()

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

func refresh(sender: AnyObject) {
   if isFirstLoad == false {
      currentPage = 1
       getStuff()
    }

   self.theRefreshControl.endRefreshing()
}

I also tried adding self.tableView.backgroundView = refreshControl above and below self.tableView.addSubview(theRefreshControl); and also tried replacing self.tableView.addSubview(theRefreshControl) with self.tableView.backgroundView = refreshControl but unfortunately no luck


Solution

  • The way you should instantiate the refreshControl is like so:

    override func viewDidLoad() {
        let refreshControl = UIRefreshControl()
        refreshControl.addTarget(self, action: "refresh:", forControlEvents: .ValueChanged)
        tableView.backgroundView = refreshControl // <- THIS!!!
    }
    

    as stated in this answer.

    My guess is that your refreshControl is in front of your cells, so if the above doesn't work try this: tableView.sendSubviewToBack(theRefreshControl) in viewDidLoad

    EDIT: Since tableView.sendSubviewToBack(theRefreshControl) was the solution, consider filing a bug with Apple, since it should happen automatically when you set the refreshControl as the tableView.backgroundView