繁体   English   中英

我不知道为什么我的 Tableview 在选择行时没有推送 DetailView

[英]I don't know why my Tableview did not push the DetailView when row is selected

我找到了这个示例代码,并修改了代码以使其符合我的要求。 tableview 从 plist 加载数据,它的工作数据显示在部分中,它现在在选择一行时工作,我想推送一个 Detailviewcontroller 以显示更多详细信息。 Grrrr,它不起作用,我不知道为什么。 这段代码和另一个可以正常工作的代码之间的区别在于,ICB_SectionedTableViewDemoViewController 在 appDelegate 中被声明为 class,我不知道当我想要推送 DetailViewController 时它是否会发生。

这是我的根控制器的代码,名为 ICB_SectionedTableViewDemoViewController.m

//  ICB_SectionedTableViewDemoViewController.m
//  ICB_SectionedTableViewDemo
//
//  Created by Matt Tuzzolo on 12/10/10.
//  Copyright 2010 ELC Technologies. All rights reserved.
//

#import "ICB_SectionedTableViewDemoViewController.h"
#import "ICB_SectionedTableViewDemoAppDelegate.h"
#import "DetailViewController.h"



@implementation ICB_SectionedTableViewDemoViewController

@synthesize books, sections ;

- (void)viewDidLoad {

    self.books = [NSMutableArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"books" ofType:@"plist"]];
    self.sections = [[NSMutableDictionary alloc] init];

    BOOL found;

    // Loop through the books and create our keys
    for (NSDictionary *book in self.books)
    {        
        NSString *c = [[book objectForKey:@"author"] substringToIndex:1];

        found = NO;

        for (NSString *str in [self.sections allKeys])
        {
            if ([str isEqualToString:c])
            {
                found = YES;
            }
        }

        if (!found)
        {     
            [self.sections setValue:[[NSMutableArray alloc] init] forKey:c];
        }
    }

    // Loop again and sort the books into their respective keys
    for (NSDictionary *book in self.books)
    {
        [[self.sections objectForKey:[[book objectForKey:@"author"] substringToIndex:1]] addObject:book];
    }    

    // Sort each section array
    for (NSString *key in [self.sections allKeys])
    {
        [[self.sections objectForKey:key] sortUsingDescriptors:[NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"author" ascending:YES]]];
    }    

    [super viewDidLoad];
}

#pragma mark -
#pragma mark Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{
    return [[self.sections allKeys] count];
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{    
    if(section == 0)
        return @"COULEURS";
    else
        return @"MOTIFS";    

  //  return [[[self.sections allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)] objectAtIndex:section];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{
    return [[self.sections valueForKey:[[[self.sections allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)] objectAtIndex:section]] count];
}

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
    return [[self.sections allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
}

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

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

    NSDictionary *book = [[self.sections valueForKey:[[[self.sections allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)] objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row];

    cell.textLabel.text = [book objectForKey:@"title"];    
    cell.detailTextLabel.text = [book objectForKey:@"description"];

    return cell;
}

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



    //Initialize the detail view controller and display it.
    DetailViewController *dvController = [[DetailViewController alloc] initWithNibName:@"DetailView" bundle:[NSBundle mainBundle]];
    dvController.CL = [self.books objectAtIndex:indexPath.row];
    [self.navigationController pushViewController:dvController animated:YES];
 //   [self presentModalViewController:dvController animated:YES];
 //   [self.view addSubview:dvController.view];
    [dvController release];



}


- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}

@end

感谢您非常有用的帮助,并提醒我是初学者,我的第一语言是法语。

看了项目之后,好像没有创建UINavigationController。 因此,我建议您将此代码用于application:didFinishLaunchingWithOptions:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    

    UINavigationController* navigation = [[UINavigationController alloc] init];
    [navigation pushViewController:viewController animated:NO];
    [window addSubview:navigation.view];

    [self.window makeKeyAndVisible];

    return YES;
 }

在这里,实例化了 UINavigationController,并将您的第一个(根)controller 推到它上面。

重要提示:您的应用程序有更多问题,因此无法运行,但至少您的 DetailView controller 将被加载并尝试显示。 关于我发现的问题:

  1. didSelectRowAtIndexPath中,您使用了错误的笔尖名称;

  2. DetailViewControllerviewDidLoad中,您使用的是未定义的属性CL.title

一旦您解决了这些问题,可能会显示详细信息视图。

旧答案:

tableView:didSelectRowAtIndexPath:中设置断点并检查您的DetailViewController是否已正确创建,并且self.navigationController是否为 nil。

或者,您可以将 NSLog 跟踪添加到您的代码中(这是一种重要的调试技术):

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

    //Initialize the detail view controller and display it.
    DetailViewController *dvController = [[DetailViewController alloc] initWithNibName:@"DetailView" bundle:[NSBundle mainBundle]];
    dvController.CL = [self.books objectAtIndex:indexPath.row];
    [self.navigationController pushViewController:dvController animated:YES];

    NSLog(@"navController: %@", [self.navigationController description]);
    NSLog(@"dvController.CL: %@", [dvController.CL description]);


 //   [self presentModalViewController:dvController animated:YES];
 //   [self.view addSubview:dvController.view];
   [dvController release];

}

tableview应该是uinavigationcontroller的viewcontroller。请检查。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM