Pull to refresh UITableView without UITableViewController doesn't work

I cannot find a working solution. I have tried other available solutions to implement pull to update in UITableView without UITableViewController, but nothing happens.

This is the relevant code:

@IBOutlet weak var tableView: UITableView!
var peopleRefreshControl:UIRefreshControl!

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.

    peopleRefreshControl = UIRefreshControl()
    peopleRefreshControl.attributedTitle = NSAttributedString(string: "Pull to refresh")
    peopleRefreshControl.addTarget(self, action: "refresh", forControlEvents: .ValueChanged)
    // I also tried this
    // peopleRefreshControl.addTarget(self, action: Selector("refresh"), forControlEvents: .ValueChanged)
    tableView.addSubview(peopleRefreshControl)

}

func refresh(){
    println("what!")
}

      

I see a circle spinning and Pull to Refresh text when I pull, but the Refresh function is never called. What am I doing wrong? Is there something else I should be doing? I don't do anything special with the tableView other than hiding when not in use. Could this somehow be a problem?

Edit . I updated the code to include ::, but there were no changes. I can still see the circle spinning and the text "Pull to refresh", but the text "what!" never printed.

override func viewDidLoad() {
    super.viewDidLoad()

    ...
    peopleRefreshControl.addTarget(self, action: "refresh:", forControlEvents: .ValueChanged)
    ...
}

func refresh(sender:UIRefreshControl){
    println("what!")
}

      

Is there something else I should be doing?

+3


source to share


3 answers


This is because you did not add the ":" after the word was updated in the viewDidLoad. Without it, the update function will not work. Try using this code ...



var refreshControl:UIRefreshControl!

  override func viewDidLoad()
    {
        super.viewDidLoad()

        self.refreshControl = UIRefreshControl()
        self.refreshControl.attributedTitle = NSAttributedString(string: "Pull to refresh")

//look at the : after "refresh" 
        self.refreshControl.addTarget(self, action: "refresh:", forControlEvents: UIControlEvents.ValueChanged)


        self.tableView.addSubview(refreshControl)
    }

      

0


source


I had the same problem, however I found mine was due to the scoreboard being halfway down the screen - which meant I didn't have enough room to pull out the table to trigger it.



You can try to call the update manually from the scroll view delegate - perhaps check the content of offset.y and run your method there?

0


source


Your prototypes don't quite fit the target action. The action takes the sender as an argument and you need :

on the selector:

@IBOutlet weak var tableView: UITableView!
var peopleRefreshControl:UIRefreshControl!

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.

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

func refresh(sender:AnyObject){
    println("what!")
}

      

-1


source







All Articles