-1

Here in the following code, a TableViewController is created and it's tableView and refreshControl properties are set. However even without adding the TableViewController as sub view to the View Controller view, the refresh control starts working. Was curious about how this is happening?

@IBOutlet weak var dashBoardTableView: UITableView!

let refreshControl = UIRefreshControl()

func configureRefreshControl()
{
    refreshControl.addTarget(self, action: #selector(self.pullToRefresh)  , for: .valueChanged)
    //UIRefreshControl will not work properly without embedding it in a UITableViewController
    let tableViewContainerVC = UITableViewController()
    tableViewContainerVC.tableView = dashBoardTableView
    tableViewContainerVC.refreshControl = refreshControl
}
jay
  • 3,517
  • 5
  • 26
  • 44
  • Why do you initialize another `UITableViewController`? Should be enough if you do `dashBoardTableView.refreshControl = refreshControl`. – kchromik Aug 14 '17 at 08:16
  • @kchromik dashBoardTableView is a sub class of UITableView and it doesn't have a refreshControl property. – jay Aug 14 '17 at 08:23

1 Answers1

1

It's very likely the apple developer did such thing in UITableViewController:

var refreshControl:UIRefreshControl {
    didSet{
        //......
        self.tableView.tableHeaderView?.addSubview(refreshControl)
        //......
    }
}

So the RefreshController works immediately after : tableViewContainerVC.refreshControl = refreshControl

Yun CHEN
  • 6,450
  • 3
  • 30
  • 33