I have a table view controller that implements UISearchBarDelegate and UISearchDisplayDelegate. I have a button to toggle edit view for my table view. When in edit mode, a user can select many rows and copy them, delete them, etc. If I try to enter something in the search bar, while the table view is in edit mode, the shouldReloadTableForSearchString method fires, and there I re-fetch my data from core data with an updated predicate, and return YES from the function, indicating that the table view should be reloaded. When the filtered results are presented, they are not in edit mode, even though in cellForRowAtIndexPath i can see that tableView isEditing is YES. I can not see the circles for the multiple selection. I need to be able to let the user filter down their list in order to significantly constrain the results, before selecting several items.
Why are the results not showing up in edit mode?
edit mode, without using the search bar:

Searching in edit mode produces results that are not in edit mode and not styled:

Thanks in advance,
Alex
Here is the shouldReloadTableForSearchString method:
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString {
NSPredicate *predicate = nil;
if ([searchString length])
{
NSArray* filteredTags = nil;
if ([searchString length] < 3)
{
// Check for tags starting with 1 or 2 characters.
filteredTags = [[tagResultsController fetchedObjects] filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(TagEntity* te, NSDictionary *bindings)
{
if ([[[te.name lowercaseString] substringToIndex:[searchString length]] isEqualToString:[searchString lowercaseString]])
return YES;
return NO;
}]];
}
else
{
// Check for tags with 3 or more consecutive characters anywhere in name
filteredTags = [[tagResultsController fetchedObjects] filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(TagEntity* te, NSDictionary *bindings)
{
if ([[te.name lowercaseString] rangeOfString:[searchString lowercaseString]].location != NSNotFound)
return YES;
return NO;
}]];
}
// OR ANY(self.tags) in %@
predicate = [NSPredicate predicateWithFormat:
@"ANY(self.songlists) in %@ AND (name contains[cd] %@ OR artist_name contains[cd] %@ OR number = %@ OR ANY(tag_entities) in %@)",
currentSonglist.songs, searchString, searchString, searchString,filteredTags];
[self.fetchedResultsController.fetchRequest setPredicate:predicate];
NSError *error = nil;
if (![[self fetchedResultsController] performFetch:&error]) {
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
// Return YES to cause the search result table view to be reloaded.
}
return YES;
}
And here is my Core Data table view’s header from the .h file:
@interface SonglistTVC : CoreDataTableViewController
<UISearchBarDelegate,
UISearchDisplayDelegate,
UIPickerViewDataSource,
UIPickerViewDelegate,
UIActionSheetDelegate,
AddSongTVCDelegate,
ListSelectorDelegate>
I know it’s using my table view controller, because both cellForRowAtIndexPath and didSelectRowAtIndexPath are firing. I can debug inside and isEditing is set to YES on the table view. But yet the table view is not reflecting it’s proper edit mode styling.
Here is cellForRowAtIndexPath which fires immediately after typing a letter in the search box.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *identifier = @"SongCell";
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:identifier];
Song* song = [self.fetchedResultsController objectAtIndexPath:indexPath];
UILabel* artistLabel = (UILabel*)[cell viewWithTag:ARTIST_NAME_TAG];
UILabel* songLabel = (UILabel*)[cell viewWithTag:SONG_NAME_TAG];
UILabel* keyLabel = (UILabel*)[cell viewWithTag:KEY_TAG];
UILabel* numberLabel = (UILabel*)[cell viewWithTag:NUMBER_TAG];
//artistLabel.font = [UIFont systemFontOfSize:17];
[artistLabel setText: song.artist_name];
[songLabel setText: song.name];
//[songLabel setFont:[UIFont systemFontOfSize:16]];
[keyLabel setText: song.key];
[numberLabel setText: [NSString stringWithFormat:@"%@", song.number]];
UIView *backView = [[UIView alloc] initWithFrame:CGRectMake(0,0,320,61)];
UIView *selectedBackView = [[UIView alloc] initWithFrame:CGRectMake(0,0,320,61)];
CAGradientLayer *backGradient = [[CAGradientLayer alloc] init];
backGradient.frame = cell.layer.bounds;
CAGradientLayer *selectedGradient = [[CAGradientLayer alloc] init];
selectedGradient.frame = cell.layer.bounds;
if (song.status == [NSNumber numberWithUnsignedInt: 1])
{
[backGradient setColors:[NSArray arrayWithObjects:
(id)[[UIColor colorWithRed:240.0f/255.0f green:200.0f/255.0f blue:200.0f/255.0f alpha:1.0f] CGColor],
(id)[[UIColor colorWithRed:1 green:1 blue:1 alpha:1.0f] CGColor],
nil]];
}
else if (song.status == [NSNumber numberWithUnsignedInt: 2])
{
[backGradient setColors:[NSArray arrayWithObjects:
(id)[[UIColor colorWithRed:240.0f/255.0f green:240.0f/255.0f blue:200.0f/255.0f alpha:1.0f] CGColor],
(id)[[UIColor colorWithRed:1 green:1 blue:1 alpha:1.0f] CGColor],
nil]];
}
else if (song.status == [NSNumber numberWithUnsignedInt: 3])
{
[backGradient setColors:[NSArray arrayWithObjects:
(id)[[UIColor colorWithRed:200.0f/255.0f green:240.0f/255.0f blue:200.0f/255.0f alpha:1.0f] CGColor],
(id)[[UIColor colorWithRed:1 green:1 blue:1 alpha:1.0f] CGColor],
nil]];
}
[selectedGradient setColors:[NSArray arrayWithObjects:
(id)[[UIColor colorWithRed:255.0f/255.0f green:160.0f/255.0f blue:60.0f/255.0f alpha:1.0f] CGColor],
(id)[[UIColor colorWithRed:1 green:1 blue:1 alpha:0.7f] CGColor],
nil]];
if (![self.tableView isEditing] )
{
cell.backgroundView = backView;
[cell.backgroundView.layer insertSublayer:backGradient atIndex:1];
cell.selectedBackgroundView = selectedBackView;
[cell.selectedBackgroundView.layer insertSublayer:selectedGradient atIndex:1];
}
else
{
//cell.backgroundView = backView;
//[cell.backgroundView.layer insertSublayer:backGradient atIndex:1];
cell.selectedBackgroundView = backView;
[cell.selectedBackgroundView.layer insertSublayer:backGradient atIndex:1];
}
for (NSIndexPath* ip in selectedIndexes)
{
[self.tableView selectRowAtIndexPath:ip animated:YES scrollPosition:UITableViewScrollPositionNone];
}
[sortOrderPicker reloadAllComponents];
[self sendPickerDownAnimated:NO];
return cell;
}
I use a button inside a segmented control to toggle editing ON/OFF:
- (IBAction)segmentChanged:(id)sender{
UISegmentedControl* sg = (UISegmentedControl*)sender;
if (sg.selectedSegmentIndex == 0)
{
// Edit
if (![self.tableView isEditing])
{
[self.tableView setEditing:YES animated:YES];
[self.navigationController setToolbarHidden:NO animated:YES];
}
else
{
[self.tableView setEditing:NO animated:YES];
[self.navigationController setToolbarHidden:YES animated:YES];
selectedIndexes = nil;
}
[self.tableView reloadData];
}
else
{
// Sort
if (pickerShown)
[self sendPickerDownAnimated:YES];
else
[self bringPickerUpAnimated:YES];
}
}
The problem lies in the fact that UISearchDisplayController does so much for you automatically. It creates its own table view to present the search results, and manages that table view. You can set yourself as that table view’s data source and delegate, and by default that’s done for you automatically, but it isn’t your table view and you are not controlling it. If you want a greater hand is specifying what table view this is to be, you need to set the search display controller’s
searchResultsTableView; or you can get a reference to it as thing go along (e.g. thru the UISearchDisplayController’s own delegate methods). But even so you will not be the UITableViewController that manages the results table view; that’s completely internal to UISearchDisplayController. If you don’t like that, one easy solution is: don’t use UISearchDisplayController – think of another interface.