Splitviewcontroller有两个tableviews,委托问题

我有,似乎是一个基本的要求。 我正在使用xcode 4的模板制作一个splitview iPad应用程序。 我希望我的根视图控制器是一个填充了语言的表视图,我的详细视图是另一个每次用户选择左侧语言时重新填充的tableview。 问题是,当用户在rootview中选择左边的语言时,我的[tableView reloadData]; 在detailview中的function不起作用。 tableView委托不会被调用。 我需要它,以便当用户选择语言时,tableView会刷新。

这是我现在的代码: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; } 

顺便说一下,所有这些日志都正常工作(除了tableView委托方法中的那个),我已经在IB,.h和.m中设置了两个tableViews的委托。 作为测试,我在detailView nib文件中设置了一个带有IBAction的按钮,如下所示:

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

它的工作原理。 到底是怎么回事?

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath你应该更新当前的DetailViewController而不是新的。 使用拆分视图时应遵循的方法。 所以你应该替换alloc + init:

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

 DetailViewController *detObj = self.currentDetailViewController; 

其中self.currentDetailViewController应指向拆分视图中的当前左视图。