Reputation: 969
I plan to add holidays (rows) under appropriate separators (months as sections). so far I can retrieve my data from a plist and created sections with predefined theme (for 12 months), But I cannot figure out the right way to add my holidays under appropriate months.
@synthesize event, sections;
- (void)viewDidLoad {
self.event = [NSMutableArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"2013" ofType:@"plist"]];
self.sections = [[NSMutableDictionary alloc] init];
BOOL found;
for (NSDictionary *oneEvent in self.event)
{
NSString *c = [[oneEvent objectForKey:@"date"] substringToIndex:3];
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];
}
}
for (NSDictionary *oneEvent in self.event)
{
[[self.sections objectForKey:[[oneEvent objectForKey:@"date"] substringToIndex:3]] addObject:oneEvent];
}
[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
{
NSArray *months = [[NSArray alloc]initWithObjects:@"January",@"February",@"March",@"April",@"May",@"June",@"July",@"August",@"September",@"October",@"November",@"December", nil];
return [months objectAtIndex:section];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [[self.sections valueForKey:[[self.sections allKeys] objectAtIndex:section]] count];
}
- (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 *results = [[self.sections valueForKey:[[self.sections allKeys] objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row];
cell.textLabel.text = [results objectForKey:@"date"];
cell.detailTextLabel.text = [results objectForKey:@"event"];
return cell;
}
Current Result :
Upvotes: 0
Views: 82
Reputation: 2830
In the cellForRowAtIndexPath method you are assuming that [self.sections allKeys] has the same ordering as your hardcoded "months" array. One way to fix this is to keep the "months" array around as a property, then change that line to this:
NSDictionary *results = [[self.sections valueForKey:[[self.months objectAtIndex:indexPath.section] substringToIndex:3]] objectAtIndex:indexPath.row];
Probably a better way would be to store everything in an array instead of a dictionary. I'd probably use an array of 12 dictionaries, each of which has "month" and "holidays" fields. Something like this:
self.sections = @[ @{@"month":@"January",@"holidays":@[…]}, @{@"month":@"February",@"holidays":@[…]},...]
Upvotes: 1