It looks like you're new here. If you want to get involved, click one of these buttons!
#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);
}
#pragma mark - Table view data source
- (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
Replies
What's the error, and on what line?
Off the top of my head, are you assuming that when you write a mutable array to a plist, it will come back as a mutable array? It won't. Array objects that you save to a plist or NSArchive are read back as immutable object. You have to use the mutableCopy method to create a mutable copy out of the immutable version you read from the plist.
Duncan C
WareTo
Animated GIF created with Face Dancer, available for free in the app store.
- Spam
- Abuse
- Troll
0 · Off Topic Insightful Disagree Dislike Like Awesome- Spam
- Abuse
- Troll
0 · Off Topic Insightful Disagree Dislike Like AwesomeWhich line is throwing the error?
Duncan C
WareTo
Animated GIF created with Face Dancer, available for free in the app store.
- Spam
- Abuse
- Troll
0 · Off Topic Insightful Disagree Dislike Like AwesomeBasically, everything was working fine when I used a model-object style of holding my data in NSMutableArray. Now that I am using a plist this seems to have changed everything. This code above does not work now that I am using a plist. Am I way off here?
- Spam
- Abuse
- Troll
0 · Off Topic Insightful Disagree Dislike Like AwesomeThe error you reported does not make sense with the code that you posted. The error says that you are sending a message "city" to a dictionary object. The code you posted in your viewDidLoad method does not send a "city" message to anything. I see you sending the message "cityName" to "currentCity". if currentCity is a dictionary object, that code would crash, but I would expect it to show an error about the object not responding to "cityName".
Duncan C
WareTo
Animated GIF created with Face Dancer, available for free in the app store.
- Spam
- Abuse
- Troll
0 · Off Topic Insightful Disagree Dislike Like Awesome