Hi,
I have integrated the search function to my app to query core data/.sqlite and it works ok. But I have a problem and I am not sure which class configuration should I look at, could someone points me to the light, thanks
Basically my model is this
TableView 1
Display Product Category
selectRow --> TableView2
TableView 2
Display Products of selected Category
selectRow --> TableView3
As I integrated the UISearchBar in
TableView 1, I wish to have the function when people search the product they want and it will show up the
product's name right away in the table view. I tried, but the result is showing the
Category which contains the "searched product".
So, how could I get this to show up correctly and which section of configuration should I look at? Is it the configure cell part? And also, since
TableView 1 is a sectioned table view, how could I get that to display correctly when performing search
Table View section
Code:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
if (self.searchIsActive) {
return [self.filteredListContent count];
} else {
return [[fetchedResultsController sections] count];
}
}
Now is obviously wrong...
Configure cell, hopefully can display Category when search not active and display Products when search is active
Code:
- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath {
NSManagedObject *entity = nil;
if (self.searchIsActive){
// Configure the cell to show the searched item's name
entity = [[self filteredListContent] objectAtIndex:[indexPath row]];
cell.textLabel.textColor = [UIColor blackColor];
} else {
// Configure the cell to show the category's name
cell.textLabel.textColor = [UIColor blackColor];
entity = [fetchedResultsController objectAtIndexPath:indexPath];
}
cell.textLabel.text = [entity valueForKey:@"nameEN"];
}
The search predicate
Code:
- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
NSPredicate * predicate = [NSPredicate predicateWithFormat:@"ANY products.nameEN CONTAINS[cd] %@, searchText];
self.filteredListContent = [[[self fetchedResultsController] fetchedObjects] filteredArrayUsingPredicate:predicate];
}
The core database structure is
Category
nameEN
products --- one to many relations --> Product
Product
nameEN
spec
category --- many to one relation -- > Category
THANK YOU