saikamesh
saikamesh

Reputation: 4629

iOS two UITableViews on storyboard how to handle

In my iPad app storyboard design, there are two table views added.

One table view is for displaying the folder names and the other table view is for displaying the file names. When ever a folder table view cell is selected the files inside the selected folder needs to to be displayed in the other table view(the files table view).

My problem is

I am confused about

and

Please help !

Upvotes: 2

Views: 2456

Answers (2)

Zoltán Matók
Zoltán Matók

Reputation: 4045

First off: Why don't you just display the files on a pushed viewController which takes up the whole screen? Seems more intuitive for me.

If you want to do it with two tableViews, and assuming they use dynamic cells than:
1, In the .h file of your view controller

specify two tableView properties like so:

@property (weak, nonatomic) IBOutlet UITableView *foldersTableView;
@property (weak, nonatomic) IBOutlet UITableView *filesTableView;

implement the two delegate protocols of the UITableVIew

@interface ViewController : UIViewController <UITableViewDataSource, UITableViewDelegate> 

2, Add the two UITableViews onto your ViewController, and...

  • ...link your Outlets to the two tableviews
  • ...on the attributes inspector set the Tag of the foldersTableView to 1 and the filesTableView to 2
  • ...select each of the UITableViews, go to the Connections Inspector and link their two delegate methods (delegate and datasource) to your ViewController (for both of them)

3, In the .m file of your ViewController implement the three datasource methods of UITableView like so:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    if (tableView.tag == 1) {
       return theNumberOfSectionsYouWantForTheFolderTableView;
    } else {
       return theNumberOfSectionsYouWantForTheFilesTableView;
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    if (tableView.tag == 1) {
       return [foldersArray count];
    } else {
       return [filesArray count];
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
        if (tableView.tag == 1) {
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    // Configure the cell...
    return cell;

} else {
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    // Configure the cell...

    return cell;
}
}

4, Implement the selection:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView.tag == 1) {
        //fill filesArray with the objects you want to display in the filesTableView...
        [filesTableView reloaddata];
    } else {
        //do something with the selected file
    }
}

Hope I got everything correctly. Don't forget to @synthesize the properties in the .m file if you are using pre XCode 4.4.

Upvotes: 5

lohiaguitar91
lohiaguitar91

Reputation: 522

If you are using multiple tables, don't forget to hide as appropriate:

- (void)viewWillAppear:(BOOL)animated
{
    [self.table2 setHidden:YES];
    [self.table1 setHidden:NO];
    // Probably reverse these in didSelectRow
}

Upvotes: 0

Related Questions