Frage

I am trying to search for names using NSPredicate in my Code. The search works but it does not return the appropriate results. When i search for a name e.g "Colin" it returns all the other names in the table or another name like "Mike" but if i enter a random string that does not exist, it returns :"No Result Found". When i type a name in the search bar (e.g Lisa) , i expect it to find the name (Lisa) and return it but it doesn't do that

This is my code:

- (void)filterData {

[self.filteredSearch removeAllObjects];

 NSPredicate *predicate = [NSPredicate predicateWithFormat:@"self CONTAINS [cd] %@",  mySearchBar.text];

self.filteredSearch = [[self.name filteredArrayUsingPredicate:predicate] mutableCopy];

}

self.name returns all the names in the table and it gets this from a property list

NSString *path = [[NSBundle mainBundle] pathForResource:@"lecturers" ofType:@"plist"];


lecturers= [[NSDictionary alloc] initWithContentsOfFile:path];

// Allocate key objects from lecturers list
name = [lecturers objectForKey:@"name"];


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

[self filterData];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
if (tableView == self.tableView) {
    return self.name.count;
}
[self filteredSearch];

return self.filteredSearch.count;
}

How can I fix this? I have tried using self MATCHES and other NSPredicate formats but they don't seem to work.

War es hilfreich?

Lösung

Please confirm ' searchText ' have values. print searchtext in console window.

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

self.filteredSearch removeAllObjects];

 NSPredicate *predicate = [NSPredicate predicateWithFormat:@"self CONTAINS[cd]%@",  searchText];

self.filteredSearch = [[self.name filteredArrayUsingPredicate:predicate] mutableCopy];

[self.tableView reloadData];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
 return self.filteredSearch.count;
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top