简体   繁体   中英

Pull to refresh UITableView without UITableViewController is not working

I can't seem to find a working solution. I've tried the other available solutions to implement pull to refresh in a UITableView without a 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 can see the circle spinning and the text "Pull to refresh" when I pull down, but the function "refresh" is never called. What am I doing wrong? Is there something else that I should be doing? I'm not doing anything special with the tableView other than hiding it when it's not being used. Could that somehow be a problem?

Edit : I updated the code to include ":" as follows but there was absolutely no change. I still see the circle spinning and the text "Pull to refresh" but the text "what!" is never printed out.

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?

Thats because you did not add a ":" after the word refresh in the viewDidLoad. Without it, the refresh function won't run. 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)
    }

I had the same issue, however I found that mine was due to the tableview being halfway down the screen - this meant that i didn't have enough room to pull the table enough to trigger it.

You could try calling the refresh manually from the scroll view delegate - perhaps checking the content offset.y and firing your method in there?

Your prototypes are not quite right for the target action. The action takes the sender as the argument and you need a : 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!")
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM