[英]Search bar with array from dictionary
我試圖在我的應用程序中實現搜索欄,但遇到一些問題。 我的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 & 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 & 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>
您可以使用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];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.