Maximus S
Maximus S

Reputation: 11095

Having two UITableViews in one UITableViewController

I want to have two different UITableViews in one UITableViewController, and only one of them will be shown at a time. The parent view controller works as their datasource. I load data for both of the tables when the VC first loads so that the user doesn't have to wait when switching between the tables.

I add the second table view programatically, but then I lose all the styles that I implemented for the first table view, which I did in storyboard. More importantly, I don't have a prototype cell for the second table view.

Question: How do I clone the first tableView to make a second tableView?

CurrentCode:

//    Set up Recent Posts Table
    [self.recentPostsTable setDelegate:self];
    [self.recentPostsTable setDataSource:self];
    [self.recentPostsTable setTag:0];

//    Set up hot Posts Table
    UITableView *hotPostsTable = [[UITableView alloc] initWithFrame:self.recentPostsTable.frame style:self.recentPostsTable.style];
    [hotPostsTable setDelegate:self];
    [hotPostsTable setDataSource:self];
    [hotPostsTable setTag:1];
    self.hotPostsTable = hotPostsTable;

    [self fetchPosts:RECENT_POSTS_TABLE_TAG limit:[NSNumber numberWithInteger:NUM_RECENT_POSTS_IN_ONE_BATCH] skip:@0];
    [self fetchPosts:HOT_POSTS_TABLE_TAG limit:[NSNumber numberWithInteger:NUM_HOT_POSTS_IN_ONE_BATCH] skip:@0];

Upvotes: 1

Views: 33

Answers (1)

keithbhunter
keithbhunter

Reputation: 12324

It sounds like you don't actually want 2 UITableViews, but rather 2 UITableViewDataSources. Make two classes that each implement the UITableViewDataSource methods in the way that you were going to make the separate table views behave. Then when you want to "switch between table views", simply assign one of the 2 data sources to self.tableView.dataSource.

Upvotes: 1

Related Questions