簡體   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