我有以下代码:
//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;
}
}
当我在UITableView中点击一行时,我希望这会向日志输出交替值1和0。相应地,展开并折叠我点击的行。
由于某种原因,NSDictionary中从不包含任何键/值对,因此行永远不会扩展。从heightForRow方法记录如下:
2012-02-08 11:36:30.291 MappApp[5040:707] Key: 0
2012-02-08 11:36:30.293 MappApp[5040:707] Value: (null)
来自didSlectRowAt方法的记录如下:
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)
我做错了什么?
答案 0 :(得分:0)
如果expandedSections
为nil
,则记录输出有意义,即您从未创建过它或因某种原因重置它。
您应该首先检查您班级的初始化代码。