在一个用 JSON 填充的 NSArray 中搜索特定的标题

Posted

技术标签:

【中文标题】在一个用 JSON 填充的 NSArray 中搜索特定的标题【英文标题】:Search a NSArray filled with JSON for a specific title 【发布时间】:2013-09-11 08:26:07 【问题描述】:

我正在开发我的第一个严肃的 iphone 应用程序并第一次使用 json。 现在我的表格视图上方有一个搜索字段,我想将数据过滤到输入的搜索文本中。 JSON 如下所示:


"id":1,
"title":"Een Product",
"category":"Categorie",
"description":"Een Beschrijving",
"price":"10.00",
"image":"http:\/\/dev.smit-it.info\/APP\/LOGO\/een.png"
,

现在我希望能够按标题搜索,我正在尝试以下方法。

- (void) searchBarSearchButtonClicked:(UISearchBar *)sender

    [self.SearchbarOnDisplay resignFirstResponder];
    self.SearchbarOnDisplay.showsCancelButton=NO;

    //ToDo:Get the search results
    NSString *match = self.SearchbarOnDisplay.text;

    self.SearchIsStarted = YES;

    NSPredicate *sPredicate = [NSPredicate predicateWithFormat:@"SELF CONTAINS[cd] %@", match];
    NSArray *ArrayWithFilteredContent;

    ArrayWithFilteredContent = [self.dynamicData.DynamicRetrevedData filteredArrayUsingPredicate:sPredicate];
    [self.tableView reloadData];


现在我设置了一些断点,问题是self.dynamicData.DynamicRetrevedData 是 NIL。我不明白,因为这是 viewdidload 中的 alloc 和 init,并且数据当前显示在 tableview 中。如果它不是 nil 我如何指定它搜索标题。

希望你能帮忙,我已经尝试了 2 天

【问题讨论】:

如果你们中的一些人更希望这个项目很乐意分享它 使用项目本身会更容易,或者至少粘贴 viewDidLoad 的代码,这样我们就可以看到你在哪里以及如何分配东西。 self.dynamicData.DynamicRetrevedData 是什么?在 NSPredicate 之前先在 NSLog 中检查它 如果您在 DynamicRetrevedData 中保留一个 JSON,您需要先将其反序列化为 NSDictonary*,然后使用其中的每个值进行谓词。检查 NSJSONSerialization。 dropbox.com/s/4rjbnh3a3au1nx9/Smit%20IT.zip,在日志中检查了它,但它没有记录,所以我认为它是 nil。 【参考方案1】:

首先是你NSPredicate的模式不对,应该是

NSPredicate *sPredicate = [NSPredicate predicateWithFormat:@"title CONTAINS[cd] %@", match]; // or @"title beginswith[c] %@" for alphabetically search.

但这不是其他问题。如果您的 tableView 的数据正确显示,则首先逐行检查代码,那么它应该可以工作self.dynamicData.DynamicRetrevedData

【讨论】:

以上是关于在一个用 JSON 填充的 NSArray 中搜索特定的标题的主要内容,如果未能解决你的问题,请参考以下文章

UITableView 没有正确填充 NSArray 内容的部分

使用 AFNetworking 2.5 将 JSON 序列化为 NSArray

IOS:用 UIImageView 填充 NSArray

用 NSFetchRequest => [array count] 填充 NSArray 错误

UITableView的搜索

如何从使用 JSONKit 创建的 NSDictionary 中正确填充 UITableView NSArray?