Question

I have got fully working UISearchDisplayController implied in my UITableView made with Parse.com. Anyway when I put to search for, I always get only results starts with that letter. I want to display every data contains that letter.

For example: I put Om into my search and it searched me only : Oman and Ombudsman . I have in my UITableView also a cell named Community . You see this word also contains om but my search don`t display anything. It displays just words staring with these letters.

Can someone help me? Thank you.

@implementation TableViewController3
@synthesize MainTable;
@synthesize searchBar;
@synthesize searchController;
@synthesize searchResults;


- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.identifier isEqualToString:@"showDetaill"]) {
        {
            DetailViewController3 *sdvc = (DetailViewController3 *)[segue destinationViewController];


            if(self.searchDisplayController.active) {
                NSIndexPath *indexPath = [[self tableView] indexPathForSelectedRow];
                PFObject *object = [self.objects objectAtIndex:indexPath.row];

                object = (PFObject *)[[self searchResults]objectAtIndex:[[[[self searchDisplayController]searchResultsTableView]indexPathForSelectedRow]row]];
                sdvc.objc = object;

            }   else {

                NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
                PFObject *object = [self.objects objectAtIndex:indexPath.row];
                DetailViewController3 *detailViewController = [segue destinationViewController];
                detailViewController.objc = object;


            }

        }
    }
}



- (void)viewDidLoad
{
    [super viewDidLoad];

    self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)];

    self.tableView.tableHeaderView = self.searchBar;

    self.searchController = [[UISearchDisplayController alloc] initWithSearchBar:self.searchBar contentsController:self];

    self.searchController.searchResultsDataSource = self;
    self.searchController.searchResultsDelegate = self;
    self.searchController.delegate = self;



    self.searchResults = [NSMutableArray array];

    self.tableView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"background.png"]];
    [self.navigationController.navigationBar setBarTintColor:[UIColor lightGrayColor]];


-(void)filterResults:(NSString *)searchTerm {


        PFQuery *query = [PFQuery queryWithClassName: @"Countries"];
        [query whereKey:@"CountryTitle" hasPrefix:searchTerm];
        query.limit = 50;

        [query findObjectsInBackgroundWithTarget:self selector:@selector(callbackWithResult:error:)];
    }




- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString {
    [self filterResults:searchString];
    return YES;
}

- (void)callbackWithResult:(NSArray *)celebrities error:(NSError *)error
{
    if(!error) {
        [self.searchResults removeAllObjects];
        [self.searchResults addObjectsFromArray:celebrities];
        [self.searchDisplayController.searchResultsTableView reloadData];
    }
}

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

    if (tableView == self.tableView) {


        return self.objects.count;

    } else {

        return self.searchResults.count;

    }
}




- (void)searchDisplayController:(UISearchDisplayController *)controller didLoadSearchResultsTableView:(UITableView *)tableView
{
    tableView.rowHeight = 70.0f;
}



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

    NSString *uniqueIdentifier = @"MainCell";
    CustomCell3 *cell = nil;
    cell = (CustomCell3 *) [self.tableView dequeueReusableCellWithIdentifier:uniqueIdentifier];




    if (!cell) {
        NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"MainCell" owner:nil options:nil];

        for (id currentObject in topLevelObjects)
        {
            if([currentObject isKindOfClass:[CustomCell3 class]])
            {
                cell = (CustomCell3 *)currentObject;
                break;
            }
        }
    }

    if (tableView == self.tableView) {
        cell.MainTitle.text = [object objectForKey:@"CountryTitle"];
        cell.DescriptTitle.text = [object objectForKey:@"DescriptTitle"];
        [cell.FlagTitle setImageWithURL:[NSURL URLWithString:[object objectForKey:@"ImaURL"]]
                       placeholderImage:[UIImage imageNamed:@"placeholder.png"]];
        cell.backgroundView =  [[UIImageView alloc] initWithImage:[ [UIImage imageNamed:@"background.png"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0] ];
        cell.selectedBackgroundView =  [[UIImageView alloc] initWithImage:[ [UIImage imageNamed:@"background.png"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0] ];
    }






 if(tableView == self.searchDisplayController.searchResultsTableView) {

        PFObject *searchedUser = [self.searchResults objectAtIndex:indexPath.row];
        NSString *content = [searchedUser objectForKey:@"CountryTitle"];
        NSString *desco = [searchedUser objectForKey:@"DescriptTitle"];
        cell.DescriptTitle.text = desco;
        cell.MainTitle.text = content;
        NSString *image = [searchedUser objectForKey:@"ImaURL"];

    [cell.FlagTitle setImageWithURL:[NSURL URLWithString:image]
                   placeholderImage:[UIImage imageNamed:@"placeholder.png"]];
     cell.backgroundView =  [[UIImageView alloc] initWithImage:[ [UIImage imageNamed:@"background.png"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0] ];
     cell.selectedBackgroundView =  [[UIImageView alloc] initWithImage:[ [UIImage imageNamed:@"background.png"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0] ];



 }

return cell;
}





@end
Was it helpful?

Solution

Use - (void)whereKey:(NSString *)key containsString:(NSString *)substring instead of - (void)whereKey:(NSString *)key hasPrefix:(NSString *)prefix

-(void)filterResults:(NSString *)searchTerm {


    PFQuery *query = [PFQuery queryWithClassName: @"Countries"];
     [query whereKey:@"CountryTitle" containsString:searchTerm];
    query.limit = 50;

    [query findObjectsInBackgroundWithTarget:self selector:@selector(callbackWithResult:error:)];
}

if you use hasPrefix: then the search results will be elements starting with your search string.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top