I have the following code:
//in .h file: NSMutableDictionary* expandedSections;
//in Init method: expandedSections = [[NSMutableDictionary alloc] init];
- (void) tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath
{
NSString* key = [NSString stringWithFormat:@"%d", indexPath.row];
NSLog(@"Key: %@", key);
if ([expandedSections objectForKey:key] == nil)
{
NSLog(@"Value Should be: %@", [NSNumber numberWithBool:YES]);
[expandedSections setObject:[NSNumber numberWithBool:YES] forKey:key];
}
else
{
[expandedSections setObject:[NSNumber numberWithBool:![[expandedSections objectForKey:key] boolValue]] forKey:key];
}
NSLog(@"Value: %@", [expandedSections objectForKey:key]);
[tableView beginUpdates];
[tableView endUpdates];
}
- (float) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString* key = [NSString stringWithFormat:@"%d", indexPath.row];
NSLog(@"Key: %@", key);
NSLog(@"Value: %@", [expandedSections objectForKey:key]);
if ([expandedSections objectForKey:key] == nil || ![[expandedSections objectForKey:key] boolValue])
{
return 44;
}
else
{
return 88;
}
}
I’m expecting this to output to the log an alternating value of 1 and 0 when I tap a row in my UITableView. And correspondingly, expand and collapse the row I tap.
For some reason, the NSDictionary, never has any key/value pairs in it, so as a consequence the rows never expand. THe logging from heightForRow method comes out as:
2012-02-08 11:36:30.291 MappApp[5040:707] Key: 0
2012-02-08 11:36:30.293 MappApp[5040:707] Value: (null)
The logging from didSlectRowAt method comes out as:
2012-02-08 11:36:44.551 MappApp[5040:707] Key: 0
2012-02-08 11:36:46.530 MappApp[5040:707] Value Should be: 1
2012-02-08 11:36:50.723 MappApp[5040:707] Value: (null)
What am I doing wrong?
The logging output makes sense if
expandedSectionsisnil, i.e. you never created it or reset it for some reason.You should start by checking the initialization code for your class.