NSMutableDictionary
いくつかの sに関するいくつかの値を格納するために a を使用UITableViewCell
しているため、キーとして のインスタンスを使用し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;
}
は、テーブル ビュー内のすべての indexPathのminimumHeights
NSDictionary
も保持します。NSNumber