具有两个表视图的splitviewcontroller,委托问题 [英] Splitviewcontroller with two tableviews, delegate problem
问题描述
我有,这似乎是基本要求.我正在使用xcode 4的模板制作splitview iPad应用程序.我希望我的根视图控制器是一个填充有语言的表视图,而我的详细信息视图是另一个填充表,每次用户在左侧选择一种语言时都会重新填充.问题是,当用户在rootview中选择左侧的语言时,我的[tableView reloadData];在detailview中的功能不起作用,即. tableView委托不会被调用.我需要它,以便当用户选择一种语言时,tableView会刷新.
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.
这是我现在拥有的代码: RootViewController.m
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
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文件中设置了一个按钮,如下所示:
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?
推荐答案
在- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
中,您应该更新当前的DetailViewController
而不是新建一个.使用分割视图时应遵循的方法.
因此,您应该替换alloc + init:
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];
与
DetailViewController *detObj = self.currentDetailViewController;
其中self.currentDetailViewController
应指向拆分视图中当前的左视图.
where self.currentDetailViewController
should point on current left view in split view.
这篇关于具有两个表视图的splitviewcontroller,委托问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!