简体   繁体   English

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

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

I find this sample code and i modified the code to make it do what i want.我找到了这个示例代码,并修改了代码以使其符合我的要求。 The tableview load data from a plist, it works datas are displayed in sections, it works now when a row is selected, i want to push a Detailviewcontroller to show more details. tableview 从 plist 加载数据,它的工作数据显示在部分中,它现在在选择一行时工作,我想推送一个 Detailviewcontroller 以显示更多详细信息。 Grrrr, it does not work, and i don't know why. Grrrr,它不起作用,我不知道为什么。 The difference between this code and a other one where it works fine, is that ICB_SectionedTableViewDemoViewController is declared as a class in the appDelegate and i don't know if it as an incidence when i want to push the DetailViewController.这段代码和另一个可以正常工作的代码之间的区别在于,ICB_SectionedTableViewDemoViewController 在 appDelegate 中被声明为 class,我不知道当我想要推送 DetailViewController 时它是否会发生。

here is the code of my rootController named ICB_SectionedTableViewDemoViewController.m这是我的根控制器的代码,名为 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

thanks for your very useful help and remind that i'm beginner and my first language is Franch.感谢您非常有用的帮助,并提醒我是初学者,我的第一语言是法语。

After having a look at the project, it seems that you did not create a UINavigationController.看了项目之后,好像没有创建UINavigationController。 So, I suggest you to use this code for application:didFinishLaunchingWithOptions: :因此,我建议您将此代码用于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;
 }

Here, a UINavigationController is instantiated and your first (root) controller is pushed on to it.在这里,实例化了 UINavigationController,并将您的第一个(根)controller 推到它上面。

Important : your app has more problems, so it will not work, but at least your DetailView controller will be loaded and attempted to display.重要提示:您的应用程序有更多问题,因此无法运行,但至少您的 DetailView controller 将被加载并尝试显示。 About the issues I found:关于我发现的问题:

  1. in didSelectRowAtIndexPath you are using the wrong nib name;didSelectRowAtIndexPath中,您使用了错误的笔尖名称;

  2. in DetailViewController 's viewDidLoad you are using a property CL.title which is not defined;DetailViewControllerviewDidLoad中,您使用的是未定义的属性CL.title

once you fix those problems, possibly the detail view will show up.一旦您解决了这些问题,可能会显示详细信息视图。

OLD ANSWER:旧答案:

Set a breakpoint in tableView:didSelectRowAtIndexPath: and check that your DetailViewController is created correctly and also that self.navigationController is not nil.tableView:didSelectRowAtIndexPath:中设置断点并检查您的DetailViewController是否已正确创建,并且self.navigationController是否为 nil。

Alternatively you can add NSLog traces to your code (this is an important debugging technique):或者,您可以将 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];

}

The viewcontroller in which the tableview should be uinavigationcontroller.Please check it. tableview应该是uinavigationcontroller的viewcontroller。请检查。

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

相关问题 我的 TableView 数据源函数没有被调用,我不知道为什么 - My TableView datasource functions aren't being called and I don't know why 我不知道为什么结果不是我想的 - I don't know why the result is not my have thought 无法将tableView或collectionView中的选定图像中的imageView馈入到detailView中 - Can't feed imageView from a selected image in a tableView or collectionView into a detailView stringWithContentsOfFile无法转换我的文件,我也不知道为什么 - stringWithContentsOfFile can't convert my file and I don't know why 我的程序经常是死锁,我不知道为什么,如果我使用coredata - My program is often a deadlock,I don't know why,If I used coredata 在我的搜索显示控制器的搜索中,如何从整个表视图中知道该行? - How to know the row from the whole tableview selected in a search of my Search Display Controller? 当我的tableView在TabBar中时,pushViewController不起作用 - pushViewController don't work when my tableView is in an TabBar 在tableview单元格上调用setSelected时,为什么我的选择状态无法保留? - Why is my selected state not preserved when called setSelected on tableview cell? 滚动时为什么我的tableview抖动 - Why is my tableview jittery when I scroll 如何在已选择的行方法中的 ui tableview 中添加 uipicker 视图 - How to add uipicker view in ui tableview in did selected row method
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM