【问题标题】:Search in Array (from plist with dictionaries) with Fast enumeration or NSPredicate使用快速枚举或 NSPredicate 在数组中搜索(来自带有字典的 plist)
【发布时间】:2013-07-23 14:21:36
【问题描述】:

我的应用从 plist 文件中读取数据并在 UITableView 中显示数据

我的 plist 示例:

<array>
<dict>
      <key>name</key>
      <string>One</string>
      <key>value</key>
      <string>1</string>
</dict>
<dict>
      <key>name</key>
      <string>Two</string>
      <key>value</key>
      <string>2</string>
</dict>
<dict>
      <key>name</key>
      <string>Three</string>
      <key>value</key>
      <string>3</string>
</dict>
<dict>
      <key>name</key>
      <string>Four</string>
      <key>value</key>
      <string>4</string>
</dict>
<dict>
      <key>name</key>
      <string>Five</string>
      <key>value</key>
      <string>5</string>
</dict>
<dict>
      <key>name</key>
      <string>Six</string>
      <key>value</key>
      <string>6</string>
</dict>
</array

现在一切正常。一切都显示在我的 TableView 中,并带有正确的详细单元格。 我在 detailTextLabel 中显示值,在 TextLabel 中显示名称。

if (isSearching==true) {
    cell.textLabel.text = [searchArray objectAtIndex:indexPath.row];
//How to include the key "value" from this non-dictionary Array?
    return cell;
}

cell.textLabel.text = [[mainArray objectAtIndex:indexPath.row] objectForKey:@"name"];
cell.detailTextLabel.text = [[mainArray objectAtIndex:indexPath.row] objectForKey:@"value"];

当然还有 numbersOfRowsInSection:

if (isSearching) {
    return [searchArray count];
}
return [IDarray count];

我正在使用它从 plist 中获取一个数组:

NSMutableArray *mainArray = [[NSMutableArray alloc] initWithContentsOfFile:path-to-plist-file];

但是我想添加一个 UISearchBar 来搜索我的数据(数组)。

我已经观看并阅读了教程,但我无法让它与快速枚举或 NSPredicate 一起使用,还有更多替代方案吗?

因为我正在使用许多字典从 plist 中读取数据?

到目前为止,我对此进行了测试但没有成功:

-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
if (searchText.length==0) {
    isSearching = false;
}

else {
    isSearching=true;
    searchArray = [[NSArray alloc]init];

    NSPredicate *pre = [NSPredicate predicateWithFormat:@"SELF contains [search] %@", searchText];
    searchArray = [mainArray filteredArrayUsingPredicate:pre];
}
[tableView reloadData]; 
}

搜索时会显示“无结果”,有时会崩溃。

我也测试过:

-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
if (searchText.length==0) {
    isSearching = false;
}

else {
    isSearching=true;
    searchArray = [[NSArray alloc]init];

    for (NSString *str in mainArray) {
        NSRange searchRange = [str rangeOfString:searchText options:NSCaseInsensitiveSearch];


        if (searchRange.location !=NSNotFound) {
            [searchArray addObject:str];
        }
}
[tableView reloadData]; 
}

没用……

如何实现搜索栏来搜索数据? 以及如何在搜索显示控制器中显示值和名称?

【问题讨论】:

    标签: ios arrays nspredicate fast-enumeration


    【解决方案1】:

    您在 mainArray 中有字典数组。它不起作用:

    for (NSString *str in mainArray)
    

    试试吧:

    id mainArray = [[NSMutableArray alloc] initWithContentsOfFile:path-to-plist-file];
    NSMutableArray *names = [mainArray valueForKey:@"name"];
    NSMutableArray *values = [mainArray valueForKey:@"value"];
    

    在必要的数组中寻找

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-03-11
      • 1970-01-01
      • 2012-11-30
      • 1970-01-01
      • 2013-04-23
      • 1970-01-01
      • 1970-01-01
      • 2017-11-11
      相关资源
      最近更新 更多