2

I am coding an app and have a set up queryAndAppend functions that query a DB and append the info pulled to a set of arrays (which are then used to iterate through cells in a table view). The query functions work appropriately as they are called in the viewDidLoad(). I want a pull to refresh function to additionally call the functions (and thus update the table cells) but it is not working properly. Here is the relevant code:

var refreshControl = UIRefreshControl()
    override func viewDidLoad() {
            super.viewDidLoad()

            myQueryandAppend(completion: {
                self.tableView.reloadData()
            })

            upComingQueryandAppend(completion: {
                self.tableView.reloadData()
            })

            refreshControl = UIRefreshControl()
            refreshControl.attributedTitle = NSAttributedString(string: "Pull to refresh")
            refreshControl.addTarget(self, action: #selector(MainPageVC.refresh(_:)), for: UIControlEvents.valueChanged)

        }
        func refresh(_ sender: AnyObject){

            myQueryandAppend(completion: {
                self.tableView.reloadData()
                self.refreshControl.endRefreshing()

            })
            upComingQueryandAppend(completion:{

                self.tableView.reloadData()
                self.refreshControl.endRefreshing()

            })

        }

Not entirely sure why its not working as I have a very similar set up in a different view controller (with a different query/append function) that works properly with pull to refresh.

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
Runeaway3
  • 1,439
  • 1
  • 17
  • 43
  • r u using tableviewcontroller? and can you show myQueryandAppend code? – Sahil Nov 10 '16 at 05:52
  • 1
    Have you tried looking at https://stackoverflow.com/questions/24475792/how-to-use-pull-to-refresh-in-swift-2?noredirect=1&lq=1 ? – Kevin Nov 10 '16 at 05:54
  • @Sahil it turns out the issue was that, since I was not using a `tableViewController` and instead had a `tableView` in a regular VC, I needed the additional line of code: `tableView.addSubview(refreshControl)`. The link that Kevin provided helped a lot. – Runeaway3 Nov 10 '16 at 06:05
  • 1
    yes. you needed to add tableView.addSubview(refreshControl) if you are not using tableviewcontroller. – Sahil Nov 10 '16 at 06:07

2 Answers2

2

If the class is a UIViewController but not a UITableViewController, you need to add the refreshControl as a subview to the tableView manually:

tableView.addSubview(refreshControl)

So, viewDidLoad() should looks like this:

override func viewDidLoad() {
    super.viewDidLoad()


    myQueryandAppend(completion: {
        self.tableView.reloadData()
    })

    upComingQueryandAppend(completion: {
        self.tableView.reloadData()
    })

    refreshControl = UIRefreshControl()
    refreshControl.attributedTitle = NSAttributedString(string: "Pull to refresh")
    refreshControl.addTarget(self, action: #selector(MainPageVC.refresh(_:)), for: UIControlEvents.valueChanged)
    // here is the extra job!
    tableView.addSubview(refreshControl)
}
Ahmad F
  • 30,560
  • 17
  • 97
  • 143
2

In iOS 10+ you can just assign the refresh control directly to a tableview / scrollview's .RefreshControl property

tableView.RefreshControl = refreshControl
Ryan
  • 2,109
  • 1
  • 15
  • 19