0

I'm working on a table view search bar to search my Core Data content and show the matching content in the UITableView.

The Problem is, that whether if you type in a search text which matches with some of the Content or not the TableView shows "No Results".

Can someone tell my why?

TableViewController Code:

 (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSManagedObjectContext *context = [self managedObjectContext];

    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete object from database
        [context deleteObject:[self.cart objectAtIndex:indexPath.row]];

        NSError *error = nil;
        if (![context save:&error]) {
            NSLog(@"Can't Delete! %@ %@", error, [error localizedDescription]);
            return;
        }

        // Remove device from table view
        [self.cart removeObjectAtIndex:indexPath.row];
        [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }
}

//Searchbar
#pragma mark Content Filtering
-(void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope {
    // Update the filtered array based on the search text and scope.
    // Remove all objects from the filtered search array
    [self.filteredProductsArray removeAllObjects];
    // Filter the array using NSPredicate
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"Cart.cartProductName contains[c] %@",searchText];
    filteredProductsArray = [NSMutableArray arrayWithArray:[cartProducts filteredArrayUsingPredicate:predicate]];
}

#pragma mark - UISearchDisplayController Delegate Methods
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString {
    // Tells the table data source to reload when text changes
    [self filterContentForSearchText:searchString scope:
     [[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:[self.searchDisplayController.searchBar selectedScopeButtonIndex]]];
    // Return YES to cause the search result table view to be reloaded.
    return YES;
}

-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchScope:(NSInteger)searchOption {
    // Tells the table data source to reload when scope bar selection changes
    [self filterContentForSearchText:self.searchDisplayController.searchBar.text scope:
     [[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:searchOption]];
    // Return YES to cause the search result table view to be reloaded.
    return YES;
}

the Core Data content is in Cart.h and the attribute is cartProductName which is a NSString.

UPDATE:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    // Configure the cell...
    NSManagedObject *device = nil;
    if (tableView == self.searchDisplayController.searchResultsTableView)
        {device = [filteredProductsArray objectAtIndex:indexPath.row];}
    else
        {device = [self.cart objectAtIndex:indexPath.row]; }

    if (cell.accessoryView == nil) {
        // Only configure the Checkbox control once.
        cell.accessoryView = [[Checkbox alloc] initWithFrame:CGRectMake(0, 0, 25, 43)];
        cell.accessoryView.opaque = NO;
        cell.backgroundColor = [UIColor clearColor];

       [(Checkbox*)cell.accessoryView addTarget:self action:@selector(checkBoxTapped:forEvent:) forControlEvents:UIControlEventValueChanged];

    [cell.textLabel setText:[NSString stringWithFormat:@"%@", [device valueForKey:@"cartProductName"]]];
     //[(Checkbox*)cell.accessoryView setChecked: [((Cart*)device).checked boolValue]];

    //Accessibility

    [self updateAccessibilityForCell:cell];

return cell;
}
    return nil;
}

Update: CartTableViewController.m:

#import "CartViewController.h"
#import "Checkbox.h"
#import "Cart.h"


@interface CartViewController ()
@property (strong) NSMutableArray *cart;

@end

@implementation CartViewController {
    NSArray *cartProducts;
}

@synthesize filteredProductsArray;
@synthesize searchBar;
user255368
  • 17
  • 6

1 Answers1

0

You are giving few details to understand what the problem is but I guess your -(void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope should look like this:

[self.filteredProductsArray removeAllObjects];

NSFetchRequest *fetchRequest = // set up the fetch request for your Cart entity here...
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"cartProductName contains[cd] %@", searchText];
fetchRequest.predicate = predicate;

NSError *error = nil;
NSArray *results = [[self managedObjectContext] executeFetchRequest:fetchRequest error:&error];
if (results == nil) {
    // handle error here...
}

[self.filteredProductsArray addObjectsFromArray:results];

A very nice example on how to achieve this can be found at Searching in UITableView.

A performance consideration. When using cd the research will be slower that not using it. So, you can just using contains.

Hope that helps.

Lorenzo B
  • 33,216
  • 24
  • 116
  • 190
  • Hi thanks for your help so far. What kind of details do you need? Sadly your solution produces a few mistakes: 1.'use of undeclared identifier 'predicate', 2. 'Unexpected Interface name 'NSPredicate': expected expression' – user255368 Mar 16 '14 at 18:38
  • Did you follow my suggestion? @user255368 – Lorenzo B Mar 16 '14 at 19:13
  • The tutorial on GitHub? How can i download it? – user255368 Mar 16 '14 at 20:29
  • The snippet I provided. No fetch request has bene created. In addition, if you follow the link I provided you can take a look to source code. @user255368 – Lorenzo B Mar 16 '14 at 21:49
  • Did you implement correctly table view delegate methods? @user255368 – Lorenzo B Mar 16 '14 at 21:50
  • yes i copied the snipped into my code and get the mistakes i wrote in my previous comment. what methods do you mean? i posted my head in my question – user255368 Mar 17 '14 at 20:29