Example:
json = (
{
author = "Jeff Hawkins";
description = "Jeff Hawkins;
title = "On Intelligence";
},
{
author = "Jack Kerouac";
description = "The most;
title = "On The Road";
},
{
author = "Daniel Quinn";
description = "Winner of the;
title = Ishmael;
},
{
author = "Frank Herbert";
description = "This Hugo ;
title = Dune;
}
)
json = (
{
author = "Jeff Hawkins";
description = "Jeff Hawkins;
title = "On Intelligence";
},
{
author = "Jack Kerouac";
description = "The most;
title = "On The Road";
},
{
author = "Daniel Quinn";
description = "Winner of the;
title = Ishmael;
},
{
author = "Frank Herbert";
description = "This Hugo ;
title = Dune;
}
)
NSMutableArray *books;
NSMutableDictionary *sections;
//after return json value
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:@"title"] 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:@"title"] substringToIndex:1]] addObject:book];
}
// Sort each section array
for (NSString *key in [self.sections allKeys])
{
[[self.sections objectForKey:key] sortUsingDescriptors:[NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"title" ascending:YES]]];
}
// Table view
- (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];
}
//Search array for section side bar
- (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;
}
Result:
Result:

No comments:
Post a Comment