gpt4 book ai didi

ios - 如何从 NSDictionary 获取 IndexPath

转载 作者:行者123 更新时间:2023-11-29 00:54:07 25 4
gpt4 key购买 nike

假设我有以下 NSDictionary

dict = {
"a" = [
obj1,
obj2,
obj3
],
"b" = [
obj4,
obj5
],
"invalid" = [
obj6,
obj7,
obj8
],
"c" = [
obj9,
obj10,
obj11
]
}

此数据用于使用以下 NSArray 中的部分填充 TableView:

arr = @[@"A", @"B", @"C", @"D", @"E", @"F", @"G", @"H", @"I", @"J", @"K", @"L", @"M", @"N", @"O", @"P", @"Q", @"R", @"S", @"T", @"U", @"V", @"W", @"X", @"Y", @"Z"];

我有以下方法可以用来找到一个对象

- (void)selectRowWithId:(NSNumber *)uid {
[dict enumerateKeysAndObjectsUsingBlock:^(id _Nonnull key, id _Nonnull obj, BOOL * _Nonnull stop) {
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(id == %@)", uid];
NSArray *filteredDictArray = [obj filteredArrayUsingPredicate:predicate];
if ([filteredDictArray count] > 0) {

NSIndexPath *targetIndexPath = nil;
//trying to find the NSIndexPath here

[[self tableView] selectRowAtIndexPath:targetIndexPath animated:NO scrollPosition:UITableViewScrollPositionMiddle];
[self tableView:[self tableView] didSelectRowAtIndexPath:targetIndexPath];
}
}];
}

假设我有一个对象设置为 obj10

根据我的 NSDictionaryNSArrayNSIndexPathSection:2 Row:1

如果我只知道obj10,我如何得到这个值?

更新(解决方案)

因此,结合每个人的答案和我自己的答案背后的想法,我使用以下内容以防对某人有帮助。顺便说一句:这是针对 iOS9 的

- (void)selectRowWithId:(NSNumber *)uid {
[dict enumerateKeysAndObjectsUsingBlock:^(id _Nonnull key, id _Nonnull obj, BOOL * _Nonnull stop) {
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(id == %@)", uid];
NSArray *filteredDictArray = [obj filteredArrayUsingPredicate:predicate];
if ([filteredDictArray count] > 0) {

//trying to find the NSIndexPath here
NSIndexPath *targetIndexPath = [NSIndexPath indexPathForRow:[[dict objectForKey:key] indexOfObject:filteredPersonsArray[0]] inSection:[arr indexOfObject:key]];

[[self tableView] selectRowAtIndexPath:targetIndexPath animated:NO scrollPosition:UITableViewScrollPositionMiddle];
[self tableView:[self tableView] didSelectRowAtIndexPath:targetIndexPath];
}
}];
}

最佳答案

您的代码有点太棘手了。有时通常的循环更容易。

[dict enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL * stop) 
{
for( NSInteger index = 0; index < [obj count]; index++ )
{
if ([obj[index] isEqualToString:uid])
{
// thats the index
// Let's have a look-up for the section
NSString *sectionKey = [[key substringToIndex:1] uppercaseString]; //Typo
for( NSInteger section=0; section < [arr count]; section++ )
{
if( [arr[section] isEqualToString:sectionKey] )
{
// That's the section
}
}
}
}
}

在 Safari 中输入。

关于ios - 如何从 NSDictionary 获取 IndexPath,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37793198/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com