Reputation: 6266
Yes another UITableView question. I have a MainPageController
that subclasses UIViewController
and I have dragged a UITableView
into my xib
in interface builder. I have created a separate controller (with no xib) specifically to handle the UITableView
and other UITableViews
like it. Let's say it is named CustomTableViewController
. It subclasses UITableViewController
and conforms to UITableViewDataSource
.
Now, in my MainPageController
, I would like to create an instance of CustomTableViewController
and have it "control" the instance of UITableView that I have in my MainPageController's xib. How can this be done? I am mostly confused on what the tableView
property is on my CustomTableViewController and how to get myTableView
and myCustomTableViewController
to connect to each other.
I have tried things like myTableView.dataSource = myCustomTableViewController;
but nothing seems to happen. My dataSource methods are not even being called.
I am following this design because I want to reuse the custom controller for other table views that will I be using on other pages.
Upvotes: 1
Views: 839
Reputation: 2216
I tested this out and found it working. Here is what should be done -
1) In MainPageController, create a IBOutlet property tableView and hook it up with MainPageController xib.
2) In MainPageController, create instance of CustomTableViewController in viewDidLoad and set the datasource as CustomTableViewController instance -
self.myCustomTableTableViewController = [[CustomTableViewController alloc] init];
self.tableView.dataSource = self.myCustomTableTableViewController;
This should be enough to achieved what you have described.
MainPageController.h
#import <UIKit/UIKit.h>
@interface MainPageController : UIViewController
@property (strong, nonatomic) IBOutlet UITableView *tableView;
@end
MainPageController.m
#import "MainPageController.h"
#import "CustomTableViewController.h"
@interface ViewController ()
@property (strong, nonatomic) CustomTableViewController *customTableViewController;
@end
@implementation MainPageController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.customTableViewController = [[CustomTableViewController alloc] init];
self.tableView.dataSource = self.customTableViewController;
}
@end
Upvotes: 1
Reputation: 1844
UITableViewController's _tableView is inherent. Even if you create a new class with a xib you'll notice that the whole view is a tableview. When you drag a UITableView into a controller in your xib file or on storyboard, you are only creating a custom UITableView, and you're not going to be able to tell it to respond to another controller. The easiest pattern I can think of right now is this:
CustomTableViewController should subclass UIViewController. Define all your datasource functions inside it. Don't use a xib.
Because it is a UIViewController you can add an IBOutlet for a tableView. When you create the sublass of MainPageController, make it a subclass of CustomTableViewController, and set the tableView outlet.
In IB, set the datasource and delegate of the UITableView inside your ViewController to the class. It should conform since it is a subclass of CustomTableViewController.
Upvotes: 0