我的数据在plist中,这是一个按字母顺序排列的城市名称列表,它还包含一个电话号码,当用户选择一个行(城市名称)时,该电话号码将被推送到信息视图并显示表视图。我已经从使用NSMutableArray
到现在的plist来保存我的数据,现在我的segue无法正常工作。构建在模拟器中成功,但在单击表格单元格时会引发错误。有什么想法为什么这不正常?我在我的cellForRowAtIndex
..或我的prepareForSegue
方法中认为它是一个变量。
#import "CityListTableViewController.h"
@interface CityListTableViewController ()
@end
@implementation CityListTableViewController
@synthesize cities, sections;
-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
//ensure this is the Segue which is leading to the info view
if ([segue.identifier isEqualToString:@"ShowInfo"])
{
//now setup info controller so it can function...
//get the info instance
InfoViewController *ivc = [segue destinationViewController];
//get the selected row and city name from it
NSIndexPath *path = [self.tableView indexPathForSelectedRow];
//inform the info view of the selected city
int row = [path row];
City *c = [cities objectAtIndex:row];
[ivc setCurrentCity:[cities objectAtIndex:path.row]];
ivc.currentCity = c;
}
}
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad {
self.cities = [NSMutableArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"cities" ofType:@"plist"]];
self.sections = [[NSMutableDictionary alloc] init];
BOOL found;
// Loop through the cities and create our keys
for (NSDictionary *city in self.cities)
{
NSString *c = [[city objectForKey:@"city"] 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 cities into their respective keys
for (NSDictionary *city in self.cities)
{
[[self.sections objectForKey:[[city objectForKey:@"city"] substringToIndex:1]] addObject:city];
}
// Sort each section array
for (NSString *key in [self.sections allKeys])
{
[[self.sections objectForKey:key] sortUsingDescriptors:[NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"city" ascending:YES]]];
}
[super viewDidLoad];
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [[self.sections allKeys] count];
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
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 = @"DeptCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
NSDictionary *current = [[self.sections valueForKey:[[[self.sections allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)] objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row];
cell.textLabel.text = [current objectForKey:@"city"];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
@end