lightmania
lightmania

Reputation: 191

How to manage 2 UITableView (master and detail) in 1 UIView

I would like to display 2 UItableView on a same UIView. Important thing: when I select a row on the first table (master), I want to display the detail rows on the second table (detail).

In order to do that, I have :

I manage to display data in the first table, but not in the second, although the tableView:numberOfRowsInSection: is correct for the two tables.

When I run my app in debug mode, I don't see any call to the tableView:cellForRowAtIndexPath: method for the detail table.

I don't understand what I have missed or where I'm wrong. Anyone could help me ?

Here are parts of my code :

RootViewController.h

@interface RootViewController : UIViewController 
{
    MasterTVController * masterController;
    IBOutlet UITableView * masterTV;
    IBOutlet UITableView * detailTV;
}

RootViewController.m

-(void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    if (masterController == nil) {
        masterController = [[masterTVController alloc] init];
    }
    [masterTV setDataSource:masterController];
    [masterTV setDelegate:masterController];
    masterController.view = masterController.tableView;

    [detailTV setDataSource:masterController.detailTVController];
    [detailTV setDelegate:masterController.detailTVController];
}

MasterTVController.h

@interface MasterTVController : UITableViewController <UITableViewDataSource, UITableViewDelegate>
{
    NSMutableArray * masterArray;
    NSMutableArray * detailArray;
    DetailTVController * detailTVController;
}

MasterTVController.m

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        self.detailTVController = [[DetailTVController alloc] init];
        self.detailTVController.view = self.detailTVController.tableView;
    }
    return self;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    self.detailArray = [[self.masterArray objectAtIndex:indexPath.row] objectForKey:@"details"];
    self.detailTVController.dataArray = detailArray;
    [self.detailTVController.tableView reloadData];
}

Upvotes: 0

Views: 930

Answers (1)

rob mayoff
rob mayoff

Reputation: 385500

I suspect your detailTVController.tableView is not set to detailTV, so that last line in tableView:didSelectRowAtIndexPath: is not sending reloadData to the right table view. Try adding this in viewDidLoad:

masterController.detailTVController.tableView = detailTV;

You might also need to set masterController.tableView in a similar fashion.

Also, one of these two lines seems suspicious:

[menusTV setDataSource:masterController];
[masterTV setDelegate:masterController];

Upvotes: 1

Related Questions