2011-07-21 36 views
0

我找到了這個示例代碼,並且修改了代碼以使其符合我的要求。 從plist的tableview加載數據,它的作品 數據顯示在部分,它現在工作 現在,當選中一行時,我想推一個Detailviewcontroller來顯示更多細節。 Grrrr,它不起作用,我不知道爲什麼。 這段代碼和其他工作正常的代碼之間的區別在於,ICB_SectionedTableViewDemoViewController被聲明爲appDelegate中的一個類,我不知道當我想要推送DetailViewController時它是否爲事件。我不知道爲什麼我的Tableview在選擇行時沒有按下DetailView

這裏是一個名爲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 

感謝您的非常有用的幫助和提醒,我是初學者,我的第一語言是獐的我rootController的代碼。

回答

0

看了看項目後,似乎沒有創建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被實例化,你的第一個(根)控制器推到它。

重要:你的應用程序有更多的問題,所以它不會工作,但至少你的DetailView控制器將被加載並試圖顯示。關於我發現的問題:

  1. in didSelectRowAtIndexPath您正在使用錯誤的筆尖名稱;

  2. DetailViewControllerviewDidLoad您正在使用的屬性CL.title這是未定義;

一旦您解決了這些問題,可能會顯示詳細視圖。

OLD答:

tableView:didSelectRowAtIndexPath:設置斷點,並檢查您DetailViewController正確創建也是self.navigationController不爲零。

另外,您可以添加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]; 

} 
+0

感謝您的回答。我這樣做,但很抱歉,我在調試區看到的結果並沒有真正幫助我,它看起來像中文 – a3116b

+0

請參閱我的編輯,如果您同意,請告訴我請在控制檯窗口輸出什麼(打開它選擇當你點擊一行時,從菜單中選擇「Run/Console」)。 – sergio

+0

[鏈接](http://www.gdid9.eu/capture.png) – a3116b

0

其中tableview中應該uinavigationcontroller.Please檢查它的視圖控制器。

相關問題