Note that there are some explanatory texts on larger screens.

plurals
  1. POXcode: Search bar results loaded in search array but not showing in tableview
    primarykey
    data
    text
    <p>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:</p> <p>h file:</p> <pre><code>#import &lt;UIKit/UIKit.h&gt; #import &lt;CoreData/CoreData.h&gt; @interface iDataTable : UITableViewController &lt;UISearchBarDelegate, UISearchDisplayDelegate&gt; { 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 </code></pre> <p>m file:</p> <pre><code>#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:&amp;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:&amp;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; } </code></pre> <p>Thank you!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload