iphone - 从 JSON 中过滤 NSArray?

标签 iphone objective-c ios json filter

我正在尝试在我的应用程序中实现可搜索的表格 View ,以便有人可以在其中搜索位置并获得结果。它看起来像这样:

enter image description here

我从 genomes.com 获取资源,它提供的不仅仅是城市,还有公园、建筑物、县等。我只想显示城市的位置。

数据是一个 JSON 文件,由 JSONKit 解析。整个文件进入(最多 20 个对象),然后可搜索的表格 View 显示它。我不确定是否应该以不同方式解析 JSON 文件,或者是否应该让 TableView 仅显示所需的结果。 (在这种情况下,性能不是问题。)。 JSON 文件被转换为 NSArray。

这是数组的一部分:

 {
    adminCode1 = MA;
    adminCode2 = 027;
    adminName1 = Massachusetts;
    adminName2 = "Worcester County";
    adminName3 = "";
    adminName4 = "";
    adminName5 = "";
    continentCode = NA;
    countryCode = US;
    countryName = "United States";
    elevation = 178;
    fcl = A;
    fclName = "country, state, region,...";
    fcode = ADMD;
    fcodeName = "administrative division";
    geonameId = 4929431;
    lat = "42.2000939";
    lng = "-71.8495163";
    name = "Town of Auburn";
    population = 0;
    score = "53.40083694458008";
    timezone =         {
        dstOffset = "-4";
        gmtOffset = "-5";
        timeZoneId = "America/New_York";
    };
    toponymName = "Town of Auburn";
},

我想要做的是,如果“fcl”(在数组中看到)等于 P,那么我希望它在 TableView 中显示它。如果“fcl”是其他字符,那么我不想在 TableView 中看到它。我很确定 if 语句可以做到这一点,但我不知道如何获取它以便过滤其中的一部分。

任何帮助将不胜感激!谢谢

编辑:到目前为止,这是要搜索的代码:

    - (void)delayedSearch:(NSString*)searchString
{
    [self.geoNamesSearch cancel];
    [self.geoNamesSearch search:searchString
                        maxRows:20
                       startRow:0
                       language:nil];
}

- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
    self.searchDisplayController.searchBar.prompt = NSLocalizedStringFromTable(@"ILGEONAMES_SEARCHING", @"ILGeoNames", @"");
    [self.searchResults removeAllObjects];

    // Delay the search 1 second to minimize outstanding requests
    [NSObject cancelPreviousPerformRequestsWithTarget:self];
    [self performSelector:@selector(delayedSearch:) withObject:searchString afterDelay:0];

    return YES;
}

最佳答案

您的问题基本上是,如何从搜索栏字符串中过滤数组?如果是这样,您可以通过 UISearchBarDelegate 检测文本何时更改,然后通过数组复制包含您要查找的字符串的那些对象,即

这是您想要的委托(delegate)方法:searchBar:textDidChange:

[filterArray removeAllObjects];
for(int i = 0; i < [normalArray count]; i++){
    NSRange textRange;
    textRange =[[[[normalArray objectAtIndex:i] objectForKey:@"name"] lowercaseString] rangeOfString:[searchBarString lowercaseString]];
    //I wasn't sure which objectForKey: string you were looking for, just replace the one you want to filter.
       if(textRange.location != NSNotFound)
       {

        [filterArray addObject:[normalArray objectAtIndex:i]];

       }
}

filterTableView = YES;
[tableView reloadData];

注意 filterTableView bool 值,这是让您的 tableView 知道是正常加载还是您刚刚创建的过滤版本。您在以下位置实现:

tableView:numberOfRowsInSection:  //For number of rows.
tableView:cellForRowAtIndexPath:  //For the content of the cells.

希望这就是您要找的。

关于iphone - 从 JSON 中过滤 NSArray?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11640160/

相关文章:

ios - 开发期间初始安装标签的使用和验证

java - 带有 Appium 的 iOS Web 测试

ios - 一种用于 iOS 应用程序而不是 Storyboard的手动页面布局策略

ios - iOS 版 Google map 中的自定义用户位置点 (GMSMapview)

ios - 与 8.0.2 相比,是什么导致 8.1 中的自转发生巨大变化?

ios - 当显示删除确认按钮时,UITableViewCell.showingDeleteConfirmation 给出 NO

IOS预期表达式错误

iphone - 当我从 Xib 文件加载自定义 UITableViewCells 时发生泄漏?

java - 与ios联网

iphone - 委托(delegate)是执行此操作的首选方法吗?