3

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

senty
  • 12,385
  • 28
  • 130
  • 260
  • It works fine before the segue? – Quantaliinuxite Feb 18 '16 at 13:40
  • Yes, username button works fine before segue. I just realised that if I go to that page, and don't pull to refresh, it works fine.. However as soon as I pull down (so trigger refresh), then it fails to do the segue again on username button click. – senty Feb 18 '16 at 13:44
  • http://stackoverflow.com/questions/24475792/how-to-use-pull-to-refresh-in-swift – Mahesh reddy Feb 18 '16 at 13:52
  • @Maheshreddy I did exactly same with [this answer provided](http://stackoverflow.com/a/24476087/4705339). Also I made the refresh control working on the other page, which is working. And I am trying to do the same thing here, but not working properly. – senty Feb 18 '16 at 14:07
  • 1
    My guess is that your refreshControl is in front of your cells, so as a last effort try this: `tableView.sendSubviewToBack(theRefreshControl)` in `viewDidLoad` – Quantaliinuxite Feb 18 '16 at 14:07
  • @Quantaliinuxite Va laaa.. Your solution magically worked! Please add it as an answer and I accept the bounty – senty Feb 18 '16 at 14:15

1 Answers1

10

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

Community
  • 1
  • 1
Quantaliinuxite
  • 3,133
  • 4
  • 18
  • 32
  • Mine looks quite similar to this, except the line `tableView.backgroundView = refreshControl`. (Please check my updated answer, I just added my code in ViewDidLoad). I tried adding `tableView.backgroundView = refreshControl` both before and after `self.tableView.addSubview(theRefreshControl)` but unfortunately, no luck – senty Feb 18 '16 at 13:55
  • You have to remove `self.tableView.addSubview:`. Also, do you call `endRefreshing:`? – Quantaliinuxite Feb 18 '16 at 13:57
  • Yes, I am calling. `self.theRefreshControl.endRefreshing()`. I am adding my code in the question. Just a sec. – senty Feb 18 '16 at 13:59
  • Thanks a lot for your time!! This `sendSubviewToBack` solved my problem. So I used `tableView.addSubview(theRefreshControl)` and `tableView.sendSubviewToBack(theRefreshControl)`. I didn't use `tableView.backgroundView = refreshControl`. @Quan~, again, thanks a lot man. I'll accept the bounty when it lets me in 23hrs.. XoXo – senty Feb 18 '16 at 14:23
  • 1
    No problem, always a pleasure! :) – Quantaliinuxite Feb 18 '16 at 14:24