2011-09-27 58 views
1

我有,這似乎是一個基本的要求。我正在使用xcode 4的模板製作splitview iPad應用程序。我希望我的根視圖控制器是一個填充了語言的表視圖,而我的詳細視圖是每次用戶選擇左側語言時重新填充的另一個表視圖。問題是,當用戶在rootview左側選擇一種語言時,我的[tableView reloadData];在detailview中的功能不起作用,即。 tableView委託不會被調用。我需要它,以便當用戶選擇一種語言時,tableView被刷新。Splitviewcontroller有兩個tableviews,代表問題

這裏是我現在的代碼: 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的委託。作爲測試,我在IBAction的detailView nib文件中設置了一個按鈕,如下所示:

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

它工作。到底是怎麼回事?

回答

2

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath你應該更新當前DetailViewController而不是新的。在使用分割視圖時應遵循的方法。 所以,你應該更換頁頭+ INIT:

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

DetailViewController *detObj = self.currentDetailViewController; 

其中self.currentDetailViewController應該在拆分視圖當前左觀點。

+0

這聽起來像我應該做的實際。不過,我不知道如何指向self.currentDetailViewController。 – krosullivan

+0

該引用可能存儲在名稱爲「detailViewController」的AppDelegate類中。 – Nekto

+0

這對我來說是完美的,我指出它在我的代表中的參考。太感謝了。 – krosullivan