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. Grrrr, it does not work, and i don't know why. 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.
here is the code of my rootController named 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)se开发者_运维知识库ction
{
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. So, I suggest you to use this code for 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.
Important: your app has more problems, so it will not work, but at least your DetailView controller will be loaded and attempted to display. About the issues I found:
in
didSelectRowAtIndexPath
you are using the wrong nib name;in
DetailViewController
'sviewDidLoad
you are using a propertyCL.title
which is not defined;
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.
Alternatively you can add NSLog traces to your code (this is an important debugging technique):
- (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.
精彩评论