1

配列に複数の辞書を持つ Plist があります。

から検索することができませんPlist。以下のコードから検索する場合、objectForKey:@"aName"Sugar を入力する必要があるなどのフルネームを入力した場合にのみ検索できます..sug..または s..で検索できません。そして、plistにあるMilkを検索すると..Sugarの最初の辞書が表示され、plistを以下に示します ..objectForKey:@"aName"を検索する必要があります

plist から検索するのにどこが間違っているかを確認してください...

画像を入力してください

 - (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;
        }
4

2 に答える 2

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);
于 2013-01-11T13:27:04.683 に答える
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];
}

}
于 2013-01-11T14:01:53.420 に答える