我找到了這個示例代碼,並且修改了代碼以使其符合我的要求。 從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的代碼。
感謝您的回答。我這樣做,但很抱歉,我在調試區看到的結果並沒有真正幫助我,它看起來像中文 – a3116b
請參閱我的編輯,如果您同意,請告訴我請在控制檯窗口輸出什麼(打開它選擇當你點擊一行時,從菜單中選擇「Run/Console」)。 – sergio
[鏈接](http://www.gdid9.eu/capture.png) – a3116b