When using UISearchBar with UITableView in edit mode, search results are not in edit mode

I have a table view controller that implements UISearchBarDelegate and UISearchDisplayDelegate. I have a button to switch the editing view for my table view. When in edit mode the user can select several lines 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 starts, extract my data from the main data with the 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, although in cellForRowAtIndexPath I see that tableView isEditing is YES. I don’t see circles for multiple choice. I need the user to be able to filter their list to significantly limit the results before selecting multiple items.

Why are the results not displayed in edit mode?

without using the search bar:

edit mode, without using the search bar

A search in edit mode gives results that are not in edit mode, and not in style:

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 header header from the .h file:

  @interface SonglistTVC : CoreDataTableViewController <UISearchBarDelegate, UISearchDisplayDelegate, UIPickerViewDataSource, UIPickerViewDelegate, UIActionSheetDelegate, AddSongTVCDelegate, ListSelectorDelegate> 

I know this with my table view controller, because both cellForRowAtIndexPath and didSelectRowAtIndexPath are fired. I can debug internally, and isEditing has a value of YES on the table view. But at the same time, the presentation of the table does not reflect its editing style editing mode.

Here is cellForRowAtIndexPath, which starts immediately after entering a letter in the search field.

  - (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 enable / disable editing:

  - (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]; } 

}

+4
source share
2 answers

The problem is that UISearchDisplayController does so much for you automatically. It creates its own table view for presenting search results and manages this table view. You can configure yourself as a data source and a table delegate, and by default this is done for you automatically, but this is not your table view, and you do not control it. If you want the big hand to indicate what kind of table it is, you need to install the search display controller searchResultsTableView ; or you can get a link to it as you move (for example, through the delegate’s own methods UISearchDisplayController). But even so, you will not be the UITableViewController that controls the presentation of the result table; which is completely internal to the UISearchDisplayController. If you don't like this, a simple solution: do not use the UISearchDisplayController - think of a different interface.

+3
source

As matte said, in search mode, the UISearchDisplayController has its own UITableView, so to save the same design in the UITableView in edit mode, you need to pass it the same design parameters that you set in your self.tableView.

 -(void)searchDisplayController:(UISearchDisplayController *)controller didLoadSearchResultsTableView:(UITableView *)tableView { tableView.backgroundColor = self.tableView.backgroundColor; tableView.separatorColor = [UIColor redColor]; tableView.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0); } 
+1
source

All Articles