在NSMutableDictionary中使用NSIndexPath作为键的问题? [英] Issues using NSIndexPath as key in NSMutableDictionary?
问题描述
有没有任何特定的理由尝试使用 NSIndexPath
作为一个存储和检索 NSMutableDictionary
键可能会失败?
我原来试图这样做以存储 NSMutableDictionary
UITableView的高度(
。每次你点击一个 self.cellHeights
) UITableViewCell
,该单元格将基于存储在 NSMutableDictionary $ c $中的值在两个不同的高度之间展开或收缩c>
indexPath
:
- (CGFloat)tableView: (UITableView *)tableView
heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSNumber * heightNSNumber = [self.cellHeights objectForKey:indexPath];
if(!heightNSNumber)
{
heightNSNumber = [NSNumber numberWithFloat:100.0];
[self.cellHeights setObject:heightNSNumber forKey:indexPath];
}
return [heightNSNumber floatValue];
}
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated :是];
NSNumber * heightNSNumber = [self.cellHeights objectForKey:indexPath];
if(!heightNSNumber)
{
heightNSNumber = [NSNumber numberWithFloat:100.0];
[self.cellHeights setObject:heightNSNumber forKey:indexPath];
}
if([heightNSNumber floatValue] == 100.0)
{
[self.cellHeights setObject:[NSNumber numberWithFloat:50.0]
forKey: indexPath];
} else {
[self.cellHeights setObject:[NSNumber numberWithFloat:100.0]
forKey:indexPath];
}
[tableView beginUpdates];
[tableView endUpdates];
}
由于我不知道的原因,将单元格高度设置为 tableView:didSelectRowAtIndexPath:
通过 [self.cellHeights objectForKey:indexPath]
工作很好。但是,尝试通过 [self.cellHeights objectForKey:indexPath]
总是返回 tableView:heightForRowAtIndexPath:
nil因为看起来用于存储高度的indexPath与用于获取单元格高度的indexPath不匹配,即使它们具有与 indexPath.section
相同的值,和 indexPath.row
。因为这样,相同索引路径的新对象被添加到 self.cellHeights
(很明显,因为 self.cellHeights.count <
解决方案虽然我在讨论晚了,这是一个快速简单的解决方案,将允许您使用NSIndexPath实例作为字典键。
只需重新创建 indexPath添加以下行:
indexPath = [NSIndexPath indexPathForRow:indexPath.row inSection:indexPath.section];
Voilà。 tableView:heightForRowAtIndexPath:
在内部使用 NSMutableIndexPath
实例(如您将看到的断点)。不知怎的,在计算散列键时,这些实例似乎与 NSIndexPath
不一致。
通过将其转换回 NSIndexPath
,那么一切工作。
Is there any particular reason why attempting to store and retrieve a value in an NSMutableDictionary
using an NSIndexPath
as a key
might fail?
I originally attempted to do this in order to store an NSMutableDictionary
of UITableViewCell
heights (self.cellHeights
) for a UITableView
. Each time you tapped a UITableViewCell
, that cell would either expand or contract between two different heights based on the value stored in the NSMutableDictionary
for that particular indexPath
:
- (CGFloat)tableView:(UITableView *)tableView
heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSNumber *heightNSNumber = [self.cellHeights objectForKey:indexPath];
if (!heightNSNumber)
{
heightNSNumber = [NSNumber numberWithFloat:100.0];
[self.cellHeights setObject:heightNSNumber forKey:indexPath];
}
return [heightNSNumber floatValue];
}
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
NSNumber *heightNSNumber = [self.cellHeights objectForKey:indexPath];
if (!heightNSNumber)
{
heightNSNumber = [NSNumber numberWithFloat:100.0];
[self.cellHeights setObject:heightNSNumber forKey:indexPath];
}
if ([heightNSNumber floatValue] == 100.0)
{
[self.cellHeights setObject:[NSNumber numberWithFloat:50.0]
forKey:indexPath];
} else {
[self.cellHeights setObject:[NSNumber numberWithFloat:100.0]
forKey:indexPath];
}
[tableView beginUpdates];
[tableView endUpdates];
}
For reasons unknown to me, getting the cell height within tableView:didSelectRowAtIndexPath:
via [self.cellHeights objectForKey:indexPath]
works just fine. However, trying to get the cell height within tableView:heightForRowAtIndexPath:
via [self.cellHeights objectForKey:indexPath]
always returns nil because it seems that the indexPath used to store the height doesn't match the indexPath being used to fetch the cell height, even though they have the same values for indexPath.section
and indexPath.row
. Because of this, a new object for the "same" index path is added to self.cellHeights
(as evident since self.cellHeights.count
increases thereafter).
This does not happen when you store the cell heights in the NSMutableDictionary using the row ([NSNumber numberWithInteger:indexPath.row]
) as the key...so that's what I'm doing for now, but I'd like to understand why indexPath
isn't working as the key.
解决方案 Although I'm late in the discussion, here's a quick and simple solution that will allow you to use NSIndexPath instances as dictionary keys.
Just recreate the indexPath by adding the following line:
indexPath = [NSIndexPath indexPathForRow:indexPath.row inSection:indexPath.section];
Voilà. tableView:heightForRowAtIndexPath:
uses NSMutableIndexPath
instances internally (as you would see with a breakpoint). Somehow those instances seem uncooperative with NSIndexPath
when calculating hash keys.
By converting it back to an NSIndexPath
, then everything works.
这篇关于在NSMutableDictionary中使用NSIndexPath作为键的问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!