I implemented a Search Bar in a Xcode project to filter Core Data records. The problem is that the Seach seams to work (the search array is populated correctly, the number of rows of the tableview is shown correctly) but the Cell is empty.
Here is my code:
h file:
#import <UIKit/UIKit.h>
#import <CoreData/CoreData.h>
@interface iDataTable : UITableViewController <UISearchBarDelegate, UISearchDisplayDelegate> {
int nonume;
NSString *nume;
NSArray *arrTitle;
}
@property (strong, nonatomic) NSFetchedResultsController *fetchedResultsController;
@property (strong, nonatomic) NSManagedObjectContext *managedObjectContext;
@property (nonatomic, retain) NSString *nume;
@property (nonatomic) int nonume;
@property (nonatomic, retain) NSMutableArray *arrSearchRes;
@end
m file:
#import "iDataTable.h"
#import "AppDelegate.h"
@interface iDataTable ()
@end
@implementation iDataTable
@synthesize fetchedResultsController = __fetchedResultsController;
@synthesize managedObjectContext = __managedObjectContext;
@synthesize nume, nonume;
@synthesize arrSearchRes;
- (void)viewDidLoad{
[super viewDidLoad];
self.arrSearchRes = [NSMutableArray arrayWithCapacity:[[self.fetchedResultsController fetchedObjects] count]];
AppDelegate *appDelegate =
[[UIApplication sharedApplication] delegate];
NSManagedObjectContext *context =
[appDelegate managedObjectContext];
NSEntityDescription *entityDesc =
[NSEntityDescription entityForName:nume ///// language
inManagedObjectContext:context];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:entityDesc];
NSPredicate *pred =
[NSPredicate predicateWithFormat:@"(nume != '')"];
request.sortDescriptors = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"data" ascending:NO]];
[request setPredicate:pred];
NSError *error;
arrTitle = [context executeFetchRequest:request
error:&error];
[self.tableView reloadData];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
if (tableView == self.searchDisplayController.searchResultsTableView) {
return [self.arrSearchRes count];
}
else {
return [arrTitle count];
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell != nil)
{
NSManagedObject* matches = nil;
if (tableView == self.searchDisplayController.searchResultsTableView) {
matches = [arrSearchRes objectAtIndex:indexPath.row]; // get search data
}
else {
matches = [arrTitle objectAtIndex:indexPath.row]; // get general data
}
UILabel *cellLabel = (UILabel *)[cell viewWithTag:1];
[cellLabel setText:[matches valueForKey:@"nume"]];
}
else {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
return cell;
}
-(void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope {
AppDelegate *appDelegate =
[[UIApplication sharedApplication] delegate];
NSManagedObjectContext *context =
[appDelegate managedObjectContext];
NSEntityDescription *entityDesc =
[NSEntityDescription entityForName:nume ///// language
inManagedObjectContext:context];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:entityDesc];
NSPredicate *pred =
[NSPredicate predicateWithFormat:@"(nume != %@)", searchText];
request.sortDescriptors = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"data" ascending:NO]];
[self.arrSearchRes removeAllObjects];
[request setPredicate:pred];
NSError *error;
NSArray *arr = [context executeFetchRequest:request error:&error];
self.arrSearchRes = [[NSMutableArray alloc] initWithArray:arr];
NSLog(@"%@, %@", arrSearchRes, searchText); // ok - arrSearchRes looks ok
}
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString {
[self filterContentForSearchText:searchString scope:[[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:[self.searchDisplayController.searchBar selectedScopeButtonIndex]]];
return YES;
}
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchScope:(NSInteger)searchOption {
[self filterContentForSearchText:[self.searchDisplayController.searchBar text] scope:[[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:searchOption]];
return YES;
}
Thank you!
This worked for me: