Pull to refresh on UITableView Inside a UIViewController

Hassan Zaheer picture Hassan Zaheer · Nov 22, 2013 · Viewed 28.6k times · Source

I am trying to implement the pull to refresh functionality in my application. The architecture is such that the there is a UITableView inside a UIViewController. I want to be able to refresh the tableview on pull down. I tried the code below in the viewDidLoad method, but it does not work. Can any one tell me where am I wrong in implementation?

UIRefreshControl *refresh = [[UIRefreshControl alloc] init];
    refresh.tintColor = [UIColor grayColor];
    refresh.attributedTitle = [[NSAttributedString alloc] initWithString:@"Pull to Refresh"];
    [refresh addTarget:self action:@selector(get_vrns) forControlEvents:UIControlEventValueChanged];
    [self.vrnTable addSubview:refresh];

Answer

aksh1t picture aksh1t · Nov 22, 2013

Since you can't use a UITableViewController instead of UIViewController, try doing this :

UITableViewController *tableViewController = [[UITableViewController alloc] init];
tableViewController.tableView = self.vrnTable;

UIRefreshControl *refresh = [[UIRefreshControl alloc] init];
refresh.tintColor = [UIColor grayColor];
refresh.attributedTitle = [[NSAttributedString alloc] initWithString:@"Pull to Refresh"];
[self.refresh addTarget:self action:@selector(get_vrns) forControlEvents:UIControlEventValueChanged];

tableViewController.refreshControl = self.refresh;

Hope this helps!