UIRefreshControl without UITableViewController UIRefreshControl without UITableViewController ios ios

UIRefreshControl without UITableViewController


On a hunch, and based on DrummerB's inspiration, I tried simply adding a UIRefreshControl instance as a subview to my UITableView. And it magically just works!

UIRefreshControl *refreshControl = [[UIRefreshControl alloc] init];[refreshControl addTarget:self action:@selector(handleRefresh:) forControlEvents:UIControlEventValueChanged];[self.myTableView addSubview:refreshControl];

This adds a UIRefreshControl above your table view and works as expected without having to use a UITableViewController :)


EDIT: This above still works but as a few have pointed out, there is a slight "stutter" when adding the UIRefreshControl in this manner. A solution to that is to instantiate a UITableViewController, and then setting your UIRefreshControl and UITableView to that, i.e.:

UITableViewController *tableViewController = [[UITableViewController alloc] init];tableViewController.tableView = self.myTableView;self.refreshControl = [[UIRefreshControl alloc] init];[self.refreshControl addTarget:self action:@selector(getConnections) forControlEvents:UIControlEventValueChanged];tableViewController.refreshControl = self.refreshControl;


To eliminate the stutter that is caused by the accepted answer, you can assign your UITableView to a UITableViewController.

_tableViewController = [[UITableViewController alloc]initWithStyle:UITableViewStylePlain];[self addChildViewController:_tableViewController];_tableViewController.refreshControl = [UIRefreshControl new];[_tableViewController.refreshControl addTarget:self action:@selector(loadStream) forControlEvents:UIControlEventValueChanged];_theTableView = _tableViewController.tableView;

EDIT:

A way to add a UIRefreshControl with no UITableViewController with no stutter and retain the nice animation after refreshing data on the tableview.

UIRefreshControl *refreshControl = [UIRefreshControl new];[refreshControl addTarget:self action:@selector(handleRefresh:) forControlEvents:UIControlEventValueChanged];[self.theTableView addSubview:refreshControl];[self.theTableView sendSubviewToBack:refreshControl];

Later when handling the refreshed data...

- (void)handleRefresh:(UIRefreshControl *)refreshControl {    [self.theTableView reloadData];    [self.theTableView layoutIfNeeded];    [refreshControl endRefreshing];}


What you would try is use container view inside ViewController you are using. you can define clean UITableViewController subclass with dedicated tableview and place that in the ViewController.