从Plist中搜索

时间:2013-01-11 13:03:01

标签: iphone ios uitableview plist

我在数组中有一个包含多个词典的Plist。

我无法从Plist进行搜索。当我从下面的代码中搜索时,我只能输入objectForKey:@"aName"的全名,就像我需要输入Sugar ..我无法搜索..或者..我将不得不打满糖,如果我搜索在plist中的牛奶..它将显示我的第一个字典是Sugar和plist如下所示.. 我必须搜索才能 objectForKey:@“aName”

从plist.please检查我在哪里搜索错误...

eneter image

 - (void)viewDidLoad
 {
          [super viewDidLoad];

        if (!expandedSections)
        {
            expandedSections = [[NSMutableIndexSet alloc] init];
        }

        NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                                     NSUserDomainMask, YES); 
        NSString *documentsDirectory = [documentPaths objectAtIndex:0];
        NSString *documentPlistPath = [documentsDirectory stringByAppendingPathComponent:@"p.plist"];


        NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:documentPlistPath];



        valueArray = [dict objectForKey:@"title"];

        self.mySections=[valueArray copy];  
        NSLog(@"value array %@",self.mySections);


        }
        - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {


        if (tableView==self.searchDisplayController.searchResultsTableView)
        {
            return [self.searchResults count];
        }
        else
        {

            return [self.mySections count];
        }

         }


        - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section      
        {



        if (  !(tableView == self.searchDisplayController.searchResultsTableView) )
        {
            if ([expandedSections containsIndex:section] )
            {

                return [[[self.mySections objectAtIndex:section ] allKeys] count] ;




            }
            return 1;
         } else  
            if(tableView == self.searchDisplayController.searchResultsTableView) 
            {
                if ([expandedSections containsIndex:section] )
                {

                    NSString *key = [self.searchResults objectAtIndex:section];
                    NSArray *dataInSection = [[self.mySections objectAtIndex:section ] allKeys]   ;

                    return [dataInSection count];
                }

            }

        return 1;


        }

        - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

        static NSString *CellIdentifier = @"Cell";

        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
        }

        NSUInteger section = [indexPath section];
        NSUInteger row = [indexPath row];

        NSString *key = nil;
        if ([tableView isEqual:self.searchDisplayController.searchResultsTableView])
        {
            key = [self.searchResults objectAtIndex:section];
        }
        else{
            key = [self.mySections objectAtIndex:section];
        }





        NSDictionary *dict = [self.mySections objectAtIndex:indexPath.section];
        cell.textLabel.text = [dict.allKeys objectAtIndex:indexPath.row];
        cell.detailTextLabel.text= [dict valueForKey:[dict.allKeys objectAtIndex:indexPath.row]];


        return cell;
        }





        -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
        {
        UIView *tempView=[[UIView alloc]initWithFrame:CGRectMake(0,0,tableView.bounds.size.width,40)];
        tempView.backgroundColor=[UIColor blackColor];

        UILabel *tempLabel=[[UILabel alloc]initWithFrame:CGRectMake(0,3,tableView.bounds.size.width-10,40)];
        tempLabel.backgroundColor=[UIColor clearColor]; 
        tempLabel.textColor = [UIColor redColor]; //here you can change the text color of header.
        tempLabel.font = [UIFont fontWithName:@"ChalkboardSE-Bold" size:13];
        tempLabel.font = [UIFont boldSystemFontOfSize:13];
        NSString *key=nil;

        if ([tableView isEqual:self.searchDisplayController.searchResultsTableView])
        {
            key = [[self.mySections objectAtIndex:section]objectForKey:@"aName"];
        }
        else{

         key = [[self.mySections objectAtIndex:section]objectForKey:@"aName"];
        // return [NSString stringWithFormat:@"%@", key];
         }

        tempLabel.text=[NSString stringWithFormat:@"%@", key];
        [tempView addSubview:tempLabel];


        return tempView;
        }





        - (void)filterContentForSearchText:(NSString*)searchText 
                                 scope:(NSString*)scope
        {
        NSPredicate *resultPredicate = [NSPredicate 
                                        predicateWithFormat:@"SELF contains[cd] %@",
                                        searchText];

        self.searchResults = [self.mySections filteredArrayUsingPredicate:resultPredicate];

        }

        -(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
        {
        UISearchBar * searchBar = [controller searchBar];
        [self filterContentForSearchText:searchString scope:[[searchBar scopeButtonTitles] objectAtIndex:[searchBar selectedScopeButtonIndex]]];
        return YES;
        }

2 个答案:

答案 0 :(得分:3)

它为您提供了已过滤的NSArray,您可以在更远的地方对结果执行任何操作。

NSArray *_originalArray = ...;
/*
 * EDITED : because the thread's owner has seriously no idea what it is.
 *
 * you can get the searchText's value from anywhere else of your code
 * (i.e. from UITextField, UISearchBar, and anywhere else...)
 *
 */
NSString *searchText = ...; // EDITED : this is a parameter only, it can be set freely
NSArray *_filteredArray = [originalArray filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(id evaluatedObject, NSDictionary *bindings) {
            NSDictionary *_dataRow = (NSDictionary *)evaluatedObject;
            return ([[[_dataRow valueForKey:@"aName"] lowercaseString] rangeOfString:[searchText lowercaseString]].location != NSNotFound);
        }]];

NSLog (@"%@", _filteredArray);

答案 1 :(得分:0)

试试这可能对你有所帮助......

如果你使用searchBar,你应该使用它的委托方法,并尝试使用searchTableView方法。

例如......试试这个

 -(void)searchBar:(UISearchBar *)theSearchBar textDidChange:(NSString *)searchText  {


if([searchText length] > 0)  {
    searching = YES;
    [self **searchTableView**:yourSearchBar];  ///give call to the **searchTableView** method..

    //show your search table..
}

else  {
    searching = NO;

    //set your search table hidden
}


}



-(void)searchTableView:(UISearchBar *)searchBar  {

if (searchBar == yourSearchBar)  {
    NSString *searchText = yourSearchBar.text;

    for (NSString *sTemp in yourValueArray)  {
        NSComparisonResult result = [sTemp compare:searchText options: (NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch) range:NSMakeRange(0, [searchText length])];

        if (result == NSOrderedSame)  {
            [self.yourTemporarySearchArray addObject:sTemp];


        }
    }

    [yourSearchTable reloadData];
}

}