我使用 aNSMutableDictionary
来存储一些关于某些UITableViewCell
s 的值,所以我使用 as 的实例NSIndexPath
作为键。在 iOS 6 上一切正常,但是当我在 iOS 5 模拟器中运行完全相同的代码时,奇怪的事情发生了。所以我将字典的内容打印到控制台:
Printing description of heights:
{
"<UIMutableIndexPath 0x6a8a3d0> 2 indexes [1, 3]" = 100;
"<UIMutableIndexPath 0x6a8a3d0> 2 indexes [1, 3]" = 100;
"<UIMutableIndexPath 0x6a8a3d0> 2 indexes [1, 3]" = 100;
}
在 iOS 6 上,它看起来像这样:
Printing description of heights:
{
"<UIMutableIndexPath 0x75ca8c0> 2 indexes [0, 1]" = 44;
"<UIMutableIndexPath 0x75caa70> 2 indexes [0, 0]" = 10;
"<UIMutableIndexPath 0x75ca500> 2 indexes [1, 0]" = 100;
"<UIMutableIndexPath 0x717df70> 2 indexes [1, 1]" = 67;
"<UIMutableIndexPath 0x715a3e0> 2 indexes [1, 2]" = 67;
"<UIMutableIndexPath 0x717def0> 2 indexes [1, 3]" = 67;
}
显然,它应该是这样的!为什么 iOS 5 上的字典会为完全相同的键存储不同的值?
编辑:一些代码...
字典创建只是
self.heights = [[NSMutableDictionary alloc] init];
我使用新的下标语法设置值,例如:
self.heights[indexPath] = minHeight;
(indexPath
是一个NSIndexPath
,minHeight
是一个NSNumber
。)
当委托请求它们时,我动态设置这些值:
- (NSNumber *)heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (![indexPath isInTableView:self.tableView])
{
@throw [NSException exceptionWithName:NSInvalidArgumentException reason:@"indexPath not in tableView" userInfo:nil];
}
NSNumber *storedHeight = self.heights[indexPath];
if (storedHeight != nil)
{
return storedHeight;
}
NSNumber *minHeight = self.minimumHeights[indexPath];
self.heights[indexPath] = minHeight;
return minHeight;
}
minimumHeights
NSDictionary
还为表视图中的每个 indexPath 保存s NSNumber
。