【问题标题】:iOS: displaying JSON Array in UISearchBariOS:在 UISearchBar 中显示 JSON 数组
【发布时间】:2017-07-07 05:30:05
【问题描述】:

我有一个这种格式的 JSON 字典

{
  "382" : "sudhir kumar",
  "268" : "David  ",
  "385" : "aayush test",
  "261" : "Mike watson",
  "277" : "TestDrivers Driver",
  "381" : "sudhir kumar",
  "380" : "sudhir kumar",
  "383" : "asdfgh asdfgh",
  "376" : " "
}

我必须在 UISearchBar 中显示它。有人请帮我解决这个问题,因为当我从搜索中选择任何名称时,它的相应 ID 也已选择

我正在使用以下代码过滤名称,但不过滤其相关 ID 和名称

我的代码是:

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText;
{


    tempArray = [NSArray arrayWithArray:DriverNameArray];
    //NSString *stringToSearch = textField.text;

    tempId = [NSArray arrayWithArray:DriverIdArray];

    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF BEGINSWITH[c] %@",searchText]; // if you need case sensitive search avoid '[c]' in the predicate

    //NSPredicate * predicateid = [NSPredicate predicateWithFormat:@"SELF BEGINSWITH[c] %@",stringToSearch];

    NSArray *tempresults = [DriverNameArray filteredArrayUsingPredicate:predicate];
    if (tempresults.count > 0)
    {
        tempArray = [NSArray arrayWithArray:tempresults];
        tempId = [NSArray arrayWithArray:tempresults];
    }
    [SEarchTable reloadData];
    //return  YES;
}

【问题讨论】:

  • 你试过什么显示你的代码?
  • 请检查我更新的问题

标签: ios objective-c json uisearchbar


【解决方案1】:

您尚未发布与您已经尝试过的内容相关的代码,因此我不会发布直接为您提供问题解决方案的代码。

最好的办法是创建一个包含名称值和 ID 值的字典数组。显示数据时,按myArray[@"name"] 约定显示数组中的名称。然后,您可以获取该对象的索引,然后运行 ​​[myArray objectAtIndex:myCalculatedIndex][@"id"] 以获取 ID。

其实有很多方法可以解决这个问题,但这是其中之一。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多