【问题标题】:Splitviewcontroller with two tableviews, delegate problemSplitviewcontroller 有两个表视图,委托问题
【发布时间】:2011-09-27 11:48:13
【问题描述】:

我有,这似乎是一个基本要求。我正在使用 xcode 4 的模板制作一个 splitview iPad 应用程序。我希望我的根视图控制器是一个填充有语言的表格视图,而我的详细视图是另一个表格视图,每次用户在左侧选择一种语言时都会重新填充它。问题是,当用户在根视图中选择左侧的语言时,我的 [tableView reloadData];详细视图中的功能不起作用,即。 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 中为两个 tableView 设置了委托。作为测试,我在 detailView nib 文件中设置了一个带有 IBAction 的按钮,如下所示:

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

它有效。怎么回事?

【问题讨论】:

    标签: ios ipad uitableview uisplitviewcontroller


    【解决方案1】:

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

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

    DetailViewController *detObj = self.currentDetailViewController;
    

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

    【讨论】:

    • 这听起来像是我应该做的。但是我不确定如何指向 self.currentDetailViewController.
    • 该引用可能存储在您的 AppDelegate 类中,名称为 detailViewController
    • 这对我来说是完美的,我把它指向了我的代表中的参考。谢谢一百万。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多