krosullivan
krosullivan

Reputation: 172

Splitviewcontroller with two tableviews, delegate problem

I have, what seems to be a basic requirment. I am making a splitview iPad app using xcode 4's template. I want my root view controller to be a table view populated with languages and my detail view to be another tableview that gets re-populated every time the user selects a language on the left. The problem is, when a user selects a language on the left in the rootview, my [tableView reloadData]; function in the detailview doesn't work ie. the tableView delegates do not get called. I need it so that when the user selects a language the tableView gets refreshed.

Here is the code I have now: RootViewController.m

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

DetailViewController *detObj = [[DetailViewController alloc] init];

detObj.detailItem = [self.tableArray objectAtIndex: indexPath.row];
NSLog(@"Selected Item %@", [self.tableArray objectAtIndex: indexPath.row]);

}

DetailViewController.m

- (void)setDetailItem:(id)newDetailItem
{
NSLog(@"setDetailItem Called");
if (_detailItem != newDetailItem) {
    [_detailItem release];
    _detailItem = [newDetailItem retain];
    self.title = _detailItem; 
    NSLog(@"Detail Item %@", _detailItem);

    // Update the view.
    //[self testAction:self];
    [self configureView];
}

if (self.popoverController != nil) {
    [self.popoverController dismissPopoverAnimated:YES];
}       


}

- (void)configureView
{
// Update the user interface for the detail item.
[tableView setDataSource:self];
[tableView setDelegate:self];
NSLog(@"Configure");
[self.tableView reloadData];

}
#pragma mark - Split view support

- (void)splitViewController:(UISplitViewController *)svc willHideViewController:(UIViewController *)aViewController withBarButtonItem:(UIBarButtonItem *)barButtonItem forPopoverController: (UIPopoverController *)pc
{
barButtonItem.title = @"Languages";
NSMutableArray *items = [[self.toolbar items] mutableCopy];
[items insertObject:barButtonItem atIndex:0];
[self.toolbar setItems:items animated:YES];
[items release];
self.popoverController = pc;
}

// Called when the view is shown again in the split view, invalidating the button and popover controller.
- (void)splitViewController:(UISplitViewController *)svc willShowViewController:(UIViewController *)aViewController invalidatingBarButtonItem:(UIBarButtonItem *)barButtonItem
{
NSMutableArray *items = [[self.toolbar items] mutableCopy];
[items removeObjectAtIndex:0];
[self.toolbar setItems:items animated:YES];
[items release];
self.popoverController = nil;
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
NSLog(@"DETAIL numberOfSectionsInTableView Called");
#warning Potentially incomplete method implementation.
// Return the number of sections.
return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
#warning Incomplete method implementation.
// Return the number of rows in the section.
if(section == 0){
    return 2;
}
return 1;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {

if (section == 0) {
    return @"Documents";
}
else if (section == 1){
    return @"Video";
}
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 100;

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}

// Configure the cell...
cell.textLabel.text = @"Cell";

return cell;
}

By the way all those logs are working correctly (except for the one in the tableView delegate method) and I have set the delegates for both tableViews in IB, in the .h and in the .m. As a test I set up a button in the detailView nib file with an IBAction as follows:

- (void)testAction:(id)sender {
NSLog(@"Test CAlled");
[self.tableView reloadData];
}

and it works. What is going on?

Upvotes: 1

Views: 2177

Answers (1)

Nekto
Nekto

Reputation: 17877

In - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath you should update current DetailViewController and not making new one. It is approach that you should follow when using split view. So you should replace alloc+init :

DetailViewController *detObj = [[DetailViewController alloc] init];

with

DetailViewController *detObj = self.currentDetailViewController;

where self.currentDetailViewController should point on current left view in split view.

Upvotes: 2

Related Questions