带有字典数组的搜索栏

Posted

技术标签:

【中文标题】带有字典数组的搜索栏【英文标题】:Search bar with array from dictionary 【发布时间】:2013-12-12 17:18:45 【问题描述】:

我正在尝试在我的应用中实现搜索栏,但遇到了一些问题。 我的 Tableview 中的数据来自 PLIST,并填充到 NSMutableArray 中。 我的 PLIST 看起来像这样,我还可以使用 NSLOG 查看数组中的数据。 现在我想为我的表格视图提供搜索,以便您可以搜索“名称”并且搜索列出了所有适合输入名称的条目。但是如何让我的搜索能够搜索到所有“名称”条目?

更新:我已经这样做了,但是如果我开始在我的搜索字段中输入,则数组保持为空。

- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope

    NSPredicate *resultPredicate = [NSPredicate
                                    predicateWithFormat:@"name contains[c] %@",
                                    searchText];

    NSMutableArray *filtered = [myArray filteredArrayUsingPredicate:resultPredicate];

    NSLog(@"%@", filtered);


#pragma mark - UISearchDisplayController delegate methods
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller
shouldReloadTableForSearchString:(NSString *)searchString

    [self filterContentForSearchText:searchString
                               scope:[[self.searchDisplayController.searchBar scopeButtonTitles]
                                      objectAtIndex:[self.searchDisplayController.searchBar
                                                     selectedScopeButtonIndex]]];

    return YES;

我像这样将数据填充到我的表格视图中:

cell.textLabel.text = [[[[myArray objectAtIndex:indexPath.section]objectForKey:@"Rows"]objectAtIndex:indexPath.row]objectForKey:@"name"];

我的任何 PLIST 看起来都是这样的。

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Hot Drinks</key>
    <array>
        <dict>
            <key>title</key>
            <string>COFFEE SPECIALS</string>
            <key>Rows</key>
            <array>
                <dict>
                    <key>price</key>
                    <string>3,20 Euro</string>
                    <key>name</key>
                    <string>Espresso</string>
                    <key>link</key>
                    <string></string>
                    <key>subname</key>
                    <string></string>
                </dict>
                <dict>
                    <key>price</key>
                    <string>3,20 euro</string>
                    <key>name</key>
                    <string>Ristretto</string>
                    <key>link</key>
                    <string></string>
                    <key>subname</key>
                    <string>Short &amp; strong espresso</string>
                </dict>
                <dict>
                    <key>price</key>
                    <string>3,50 Euro</string>
                    <key>name</key>
                    <string>Espresso Macchiato</string>
                    <key>link</key>
                    <string></string>
                    <key>subname</key>
                    <string>Espresso with hot milk </string>
                </dict>
                <dict>
                    <key>price</key>
                    <string>4,80 Euro</string>
                    <key>name</key>
                    <string>Espresso Doppio</string>
                    <key>link</key>
                    <string></string>
                    <key>subname</key>
                    <string>Double espresso</string>
                </dict>
                <dict>
                    <key>price</key>
                    <string>3,50 Euro</string>
                    <key>name</key>
                    <string>Coffee</string>
                    <key>link</key>
                    <string></string>
                    <key>subname</key>
                    <string>Freshly brewed regular coffee</string>
                </dict>
                <dict>
                    <key>price</key>
                    <string>4,00 Euro</string>
                    <key>name</key>
                    <string>Cappuccino</string>
                    <key>link</key>
                    <string></string>
                    <key>subname</key>
                    <string>Espresso with hot milk and milk froth</string>
                </dict>
                <dict>
                    <key>price</key>
                    <string>4,50 Euro</string>
                    <key>name</key>
                    <string>White Coffee</string>
                    <key>link</key>
                    <string></string>
                    <key>subname</key>
                    <string>coffee with a lot of hot milk and milk froth</string>
                </dict>
                <dict>
                    <key>price</key>
                    <string>4,50 Euro</string>
                    <key>name</key>
                    <string>Latte Macchiato</string>
                    <key>link</key>
                    <string></string>
                    <key>subname</key>
                    <string>Espresso with a lot of hot milk and milk froth, served in a glass</string>
                </dict>
                <dict>
                    <key>price</key>
                    <string>0,50 Euro</string>
                    <key>name</key>
                    <string>Flavours of your choice</string>
                    <key>link</key>
                    <string></string>
                    <key>subname</key>
                    <string>Chocolate, caramel, white chocolate, hazelnut </string>
                </dict>
                <dict>
                    <key>price</key>
                    <string>6,50 Euro</string>
                    <key>name</key>
                    <string>Hot “Valrhona” chocolate</string>
                    <key>link</key>
                    <string></string>
                    <key>subname</key>
                    <string>Hot “Valrhona” chocolate prepared with milk and cream </string>
                </dict>
            </array>
        </dict>
        <dict>
            <key>title</key>
            <string>TEA SPECIALTIES</string>
            <key>Rows</key>
            <array>
                <dict>
                    <key>price</key>
                    <string>5,00 Euro</string>
                    <key>name</key>
                    <string>English Breakfast – Ceylon / St. James</string>
                    <key>link</key>
                    <string></string>
                    <key>subname</key>
                    <string>BLACK TEA</string>
                </dict>
                <dict>
                    <key>price</key>
                    <string>5,00 Euro</string>
                    <key>name</key>
                    <string>Darjeeling Summer Gold</string>
                    <key>link</key>
                    <string></string>
                    <key>subname</key>
                    <string>BLACK TEA</string>
                </dict>
                <dict>
                    <key>price</key>
                    <string>5,00 Euro</string>
                    <key>name</key>
                    <string>Classic Chai – Indien</string>
                    <key>link</key>
                    <string></string>
                    <key>subname</key>
                    <string>BLACK TEA</string>
                </dict>
                <dict>
                    <key>price</key>
                    <string>5,00 Euro</string>
                    <key>name</key>
                    <string>Assam Barie</string>
                    <key>link</key>
                    <string></string>
                    <key>subname</key>
                    <string>BLACK TEA</string>
                </dict>
                <dict>
                    <key>price</key>
                    <string>5,00 Euro</string>
                    <key>name</key>
                    <string>Earl Grey (Darjeeling)</string>
                    <key>link</key>
                    <string></string>
                    <key>subname</key>
                    <string>FLAVOURED BLACK TEA</string>
                </dict>
                <dict>
                    <key>price</key>
                    <string>5,00 Euro</string>
                    <key>name</key>
                    <string>Dung Ti Oolong – China</string>
                    <key>link</key>
                    <string></string>
                    <key>subname</key>
                    <string>OOLONG TEA</string>
                </dict>
                <dict>
                    <key>price</key>
                    <string>5,00 Euro</string>
                    <key>name</key>
                    <string>Green Dragon</string>
                    <key>link</key>
                    <string></string>
                    <key>subname</key>
                    <string>GREEN TEA</string>
                </dict>
                <dict>
                    <key>price</key>
                    <string>5,00 Euro</string>
                    <key>name</key>
                    <string>Grüner Tee Morgentau</string>
                    <key>link</key>
                    <string></string>
                    <key>subname</key>
                    <string>FLAVOURED GREEN TEA</string>
                </dict>
                <dict>
                    <key>price</key>
                    <string>5,00 Euro</string>
                    <key>name</key>
                    <string>Jasmin Gold</string>
                    <key>link</key>
                    <string></string>
                    <key>subname</key>
                    <string>FLAVOURED GREEN TEA</string>
                </dict>
                <dict>
                    <key>price</key>
                    <string>5,00 Euro</string>
                    <key>name</key>
                    <string>Pai Mu Tan Silver Typ</string>
                    <key>link</key>
                    <string></string>
                    <key>subname</key>
                    <string>WHITE TEA</string>
                </dict>
                <dict>
                    <key>price</key>
                    <string>5,00 Euro</string>
                    <key>name</key>
                    <string>Get the Power</string>
                    <key>link</key>
                    <string></string>
                    <key>subname</key>
                    <string>FLAVOURED FRUIT INFUSION</string>
                </dict>
                <dict>
                    <key>price</key>
                    <string>5,00 Euro</string>
                    <key>name</key>
                    <string>Red Fruit</string>
                    <key>link</key>
                    <string></string>
                    <key>subname</key>
                    <string>FLAVOURED FRUIT INFUSION</string>
                </dict>
                <dict>
                    <key>price</key>
                    <string>5,00 Euro</string>
                    <key>name</key>
                    <string>Herbs and Ginger</string>
                    <key>link</key>
                    <string></string>
                    <key>subname</key>
                    <string>HERBAL INFUSION</string>
                </dict>
                <dict>
                    <key>price</key>
                    <string>5,00 Euro</string>
                    <key>name</key>
                    <string>Original Roibosh</string>
                    <key>link</key>
                    <string></string>
                    <key>subname</key>
                    <string>HERBAL INFUSION</string>
                </dict>
                <dict>
                    <key>price</key>
                    <string>5,00 Euro</string>
                    <key>name</key>
                    <string>Verbena</string>
                    <key>link</key>
                    <string></string>
                    <key>subname</key>
                    <string>HERBAL INFUSION</string>
                </dict>
                <dict>
                    <key>price</key>
                    <string>5,00 Euro</string>
                    <key>name</key>
                    <string>Fruity Camomile</string>
                    <key>link</key>
                    <string></string>
                    <key>subname</key>
                    <string>HERBAL INFUSION</string>
                </dict>
                <dict>
                    <key>price</key>
                    <string>5,00 Euro</string>
                    <key>name</key>
                    <string>Mint &amp; Fresh</string>
                    <key>link</key>
                    <string></string>
                    <key>subname</key>
                    <string>HERBAL INFUSION</string>
                </dict>
                <dict>
                    <key>price</key>
                    <string>5,00 Euro</string>
                    <key>name</key>
                    <string>Equilibrium</string>
                    <key>link</key>
                    <string></string>
                    <key>subname</key>
                    <string>FLAVOURED HERBAL INFUSION</string>
                </dict>
                <dict>
                    <key>price</key>
                    <string>5,00 Euro</string>
                    <key>name</key>
                    <string>Fruits of Paradise</string>
                    <key>link</key>
                    <string></string>
                    <key>subname</key>
                    <string>FLAVOURED HERBAL INFUSION</string>
                </dict>
            </array>
        </dict>
    </array>
</dict>
</plist>

【问题讨论】:

【参考方案1】:

您可以使用NSPredicate

我建议如下:

NSPredicate *predicate = [NSPredicate predicateWithFormat: @"name contains[c] %@", stringFromSearchField];

您可以通过以下方式获得过滤后的数组:

NSMutableArray *rows = [NSMutableArray array];
for(NSDictionary *dict in myArray)

    NSArray *tempRows = dict[@"Rows"];
    if([tempRows isKindOfClass: [NSArray class]] && tempRows.count)
    
        [rows addObjectsFromArray:tempRows];
    

NSArray *filteredArray = [rows filteredArrayUsingPredicate: predicate];

【讨论】:

这很好用。非常感谢。结果显示正确。是否有机会在搜索结果中显示单元格的详细信息?除了名称之外,我还使用这种方式显示子名称 if (tableView == self.searchDisplayController.searchResultsTableView) cell.textLabel.text = [[filteredArray objectAtIndex:indexPath.row]objectForKey:@"name"]; cell.detailTextLabel.text = [[filteredArray objectAtIndex:indexPath.row]objectForKey:@"subname"]; NSLog(@"%@", [[filtered objectAtIndex:indexPath.row]objectForKey:@"subname"]); 您可以显示与原始表格视图中相同的数据。因为filteredArray包含完全相同的对象。 NSLOG 显示数据,但它们不显示在搜索结果中。只有名字。 问题在于您的单元格样式。 再次感谢。是的。你是对的,我是瞎子。对不起。

以上是关于带有字典数组的搜索栏的主要内容,如果未能解决你的问题,请参考以下文章

从带有搜索栏的表格单元格创建模式视图时,无法在导航栏中创建后退按钮

使用 alamofire 发布带有字典和字典数组的 JSON 对象

在 Swift 中搜索字典数组的值

在字典数组中搜索字符串数组

无法使用 AFNetworking 发布带有数组的字典

通过键搜索字典数组