3

I have a UIView with a connected UIViewController. As a subview (built via the interface builder), I have a UITableView. I would like to add a refresh control to that UITableView, but to do that I need to get the default controller behind the table view.

Is there a method of UITableView that I can get the UITableViewController it is associated with from?

steventnorris
  • 5,656
  • 23
  • 93
  • 174
  • There is a confusion between UIView and UIViewController. A UITableView doesn't have a UITableViewController instance associated to it. It just have a delegate and a data source, which are often a view controller. UITableViewController is just a convenience class for less setup of your UIViewController. But it remains essentially a UIViewController. – PatrickNLT Aug 28 '14 at 19:31

1 Answers1

3

Unfortunately, there is not a way to access the UITableView's controller without subclassing and setting a delegate after instantiation (but I highly, highly recommend against this). Your best bet would be to set the refresh control within the UITableViewController (in code, if possible) that you've linked up to the table view in IB.

To add a refresh control to a UITableView that is not already being controlled by a UITableViewController, you can do the following.

First, create a UITableViewController to manage the control.

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

Create the control, add a target that manages the UITableView's state after a refresh, and then assign it to the tableViewController you just created.

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

The final thing to do is to implement refreshControlValueChanged: in your UIViewController.

Dan Loewenherz
  • 10,879
  • 7
  • 50
  • 81
  • I don't have a UITableViewController linked in IB. I have a custom UIViewController linked to the rootView, but the table view is a subview of this root view. How would I progress at that point? – steventnorris Aug 28 '14 at 18:59
  • I assume that your UITableView has a delegate set for data source and UITableViewDelegate. If you have set that to the ViewController, you already have the reference to it (via delegate). – Anand Aug 28 '14 at 19:06
  • @Anand I do have a delegate set. The delegate is the custom UIViewController I use, not a UITableViewController. How do I access it from there? – steventnorris Aug 28 '14 at 19:10
  • @steventnorris why not just use a UITableViewController instead of your UIViewController? It will end up simplifying some of the code I'll be providing in this answer. – Dan Loewenherz Aug 28 '14 at 19:14
  • When you have a UITableView inside a UIViewController, you do not have a UITableViewController (unless you added a UIViewController as a childViewController of UIViewController). From your description, it only looks like that you have a UIViewController which contains a UIView subview which contains a UITableView, so there is no UITableViewController. – Anand Aug 28 '14 at 19:14
  • You have UITableViewController when you drag and drop it in the storyboard and UIViewController class is set to UITableViewController but in you case I do not think that is the case. – Anand Aug 28 '14 at 19:15
  • @Dan You cannot resize the UITableView inside UITableViewController or put any other subviews (with exception to when you want to set subview as section of the table) – Anand Aug 28 '14 at 19:17
  • 1
    @Anand That is true. I have a UIViewController with a view with a tableview as a subview. so how would I set up a refreshControl on my table at this point? – steventnorris Aug 28 '14 at 19:18
  • Try the accepted answer on this post : http://stackoverflow.com/questions/12497940/uirefreshcontrol-without-uitableviewcontroller – Anand Aug 28 '14 at 19:21
  • 1
    Other than this approach, you could also add a UITableViewController as a childViewController. And according to the documentation UITableViewController already contains it. https://developer.apple.com/library/ios/documentation/uikit/reference/UIRefreshControl_class/Reference/Reference.html – Anand Aug 28 '14 at 19:26