ios - 如何从 NSDictionary 获取 IndexPath

标签 ios objective-c nsdictionary nsindexpath

假设我有以下 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/

相关文章:

iphone - 如何传递带有 postNotificationName :object: 的 NSDictionary

ios - OCMock:使用调用的 NSDictionary 参数测试方法

ios - 如何创建自定义 TextCell 和 TextRow,其中包含 UITextField 的子类?

ios - iOS Mobile Safari滚动:减速时的DOM操作?

ios - NSLocalizedString 不默认为基本语言

iphone - 发送到实例的选择器无效 - objectForKey :

ios - 我如何修剪 NSString 中的空白(空)行?

ios - 如何使用 CKSubscription 检查 CKAsset 的变化?

objective-c - UITextView使用setContentInset时文本被截断

ios - 在 ViewController 之间使用 instantiateViewControllerWithIdentifier 传递数据