UITableview doesn't reload when searching - ios

I'm having a UISearchbar in my navigationbar. When I search something the delegate:
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString {
NSLog(#"Should reload");
[self filterContentForSearchText:searchString
scope:[[self.searchDisplayController.searchBar scopeButtonTitles]
objectAtIndex:[self.searchDisplayController.searchBar
selectedScopeButtonIndex]]];
return YES;
}
is called so It should reload my tableview. But it doesn't. I've got two results in my search array so that's not the problem.
My init in my UITableviewController is like this:
_searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
_searchBar.delegate = self;
_searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:_searchBar contentsController:self];
_searchDisplayController.delegate = self;
_searchDisplayController.searchResultsDataSource = self;
_searchDisplayController.searchResultsTableView.delegate = self;
After I alloc init my UITableviewController I do this:
_poiTableView = [[POITableViewController alloc] init];
self.navigationItem.titleView = _poiTableView.searchBar;
so the searchbar is in my navigationbar and it calls searchdislaycontroller the only thing is that it doesn't reload my tableview. Before I moved the searchbar to the navigationbar, it was in the headercell of the tableview. Then it reloaded my tableview. I only moved the searchbar tot the navigationbar.

Implement the method searchBarShouldEndEditing: and there reload the tableview with [tableView reloadData];
Also, return YES at the end of the method since the return type is BOOL. Otherwise, it won't receive the end editing delegate calls.

Related

UISearchController Not working

I am trying to implement a UISearchController in a TableViewController. After i enter the text in the search bar I am getting the count in the console of how many records to display but the tableview is not getting reloaded. Please find the code for the Update Search Results Controller.
- (void)viewDidLoad {
[super viewDidLoad];
self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
self.searchController.searchResultsUpdater = self;
self.searchController.dimsBackgroundDuringPresentation = NO;
self.searchController.searchBar.delegate = self;
self.tableView.tableHeaderView = self.searchController.searchBar;
self.definesPresentationContext = YES;
_db=[[Database alloc]init];
if(_vehicles==nil){
_vehicles=[[NSMutableArray alloc]init];
}
_vehicles=[_db getTheData:nil];
NSLog(#"%lu",(unsigned long)_vehicles.count);
}
- (void)updateSearchResultsForSearchController:(UISearchController *)searchController
{
NSString *searchText = searchController.searchBar.text;
NSMutableArray<Vehicle*> *searchResults = [[NSMutableArray<Vehicle*> alloc]init];
for(Vehicle *v in self.vehicles){
if([v.make containsString:searchText]){
[searchResults addObject:v];
}
}
NSLog(#"%lu",searchResults.count);
VehicleTableViewController *tableController = (VehicleTableViewController *)self.searchController.searchResultsController;
tableController.vehicles = searchResults;
[tableController.tableView reloadData];
}
VehicleTableViewController *tableController = (VehicleTableViewController *)self.searchController.searchResultsController;
This line is wrong I think. searchResultsController is not tableviewctrler. You cannot force it to be one. You already have tableView reference so use that.
[self.tableView reloadData];
Make sure you replace the Table datasource with the searchresult before reloading the table.
[dataSourceArr removeAllObjects];
[dataSourceArr = [NSMutableArray arrayWithArray: searchResults];

searchBar does not hide after pushing another view controller

I created a search bar at the header view of a table:
self.searchResults = [NSMutableArray arrayWithCapacity:[self.list count]];
self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
self.searchController.searchResultsUpdater = self;
self.searchController.dimsBackgroundDuringPresentation = NO;
self.searchController.hidesNavigationBarDuringPresentation = NO;
self.searchController.searchBar.barTintColor = [UIColor clearColor];
self.searchController.searchBar.frame = CGRectMake(self.searchController.searchBar.frame.origin.x, self.searchController.searchBar.frame.origin.y, self.searchController.searchBar.frame.size.width, 44.0);
[[UIBarButtonItem appearanceWhenContainedIn: [UISearchBar class], nil] setTintColor:[UIColor whiteColor]];
[self.searchController.searchBar becomeFirstResponder];
And when tap a certain row in the tableview, a new view controller will be pushed.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
NSLog(#"HEY ROW %ld", indexPath.row+1);
ZLContainerViewController *controller = [[ZLContainerViewController alloc]init]; // a container for UIPage
[self.navigationController pushViewController:controller animated:YES];
}
However, after pushing the new view controller, the search bar is still there. How can I solve this problem?
Any suggestion is much appreciated!
Use the below code in viewDidLoad.
Swift:
self.definesPresentationContext = true
Objective-C:
self.definesPresentationContext = YES;
This solved the problem for me.
One of these two UISearchBarDelegate delegate methods should fix your problem
- (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar {
return YES;
}
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
[self.searchBar resignFirstResponder];
}

UISearchBar on section header disappears after cancel on iOS7

I have a UITableViewController and I want a UISearchBar on the top so I use
viewForHeaderInSection: delegate method because when I scroll I don't want to hide the UISearchBar. The problem is (only iOS7) that when I press cancel button the UISearchBar disappears.
here is some code:
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:UITableViewStylePlain];
if (self) {
// Custom initialization
self.filteredListContent = [[NSMutableArray alloc] init];
mySearchBar = [[UISearchBar alloc] init];
mySearchBar.delegate = self;
[mySearchBar setAutocapitalizationType:UITextAutocapitalizationTypeNone];
[mySearchBar sizeToFit];
searchDisplayController = [[UISearchDisplayController alloc]initWithSearchBar:mySearchBar contentsController:self];
[searchDisplayController setDelegate:self];
[self setSearchDisplayController:searchDisplayController];
[searchDisplayController setSearchResultsDataSource:self];
self.tableView.scrollEnabled = YES;
}
return self;
}
Then I return mySearchBar on viewForHeaderInSection: delegate method.
What's wrong with that?

Search bar in top of ViewContoller with UITableView and reload data

I got ViewController and UITableViewController in it. In top of my VC I got Search bar. I need to connect Search bar with my tableView.
#interface MessageViewController : UIViewController <UITableViewDataSource, UITableViewDelegate, UISearchBarDelegate, UISearchDisplayDelegate>
//.m
self.searchDisplayController.searchBar.delegate = self;
self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 270, kRowHeight)];
self.searchBar.delegate = self;
self.searchBar.backgroundColor = [UIColor colorWithWhite:0.9 alpha:0.7];
self.searchBar.barStyle = UISearchBarStyleDefault;
[self.view addSubview:self.searchBar];
#pragma mark - UISearchDisplayController Delegate Methods
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
[self doSearch];
return YES;
}
Why I not get updated tableView?
After performing the search try to invoke [self.tableView reloadData];, at the bottom of your doSearch method to reload the content of your tableView.

Selections not occurring when UISearchDisplayController is being used

I am using UISeachDisplayController with a UITableView. It is set up programatically in the viewDidLoad method for the UITableViewController:
- (void)viewDidLoad
{
[super viewDidLoad];
[self loadProjectsAndTitles];
searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 600, 44)];
searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:searchBar
contentsController:self];
searchDisplayController.delegate = self;
searchDisplayController.searchResultsDataSource = self;
searchDisplayController.searchResultsTableView.allowsSelection = TRUE;
self.tableView.tableHeaderView = searchBar;
}
Everything is working absolutely fine except that when I click on a row when the UISearchBar is being used, the - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath method is not being called although it is being called perfectly well when the UISearch is not being used. My question is is this normal behaviour or should the selection method work? If it should work are there any suggestions why it might not be working?
I think you forgot to set
searchDisplayController.searchResultsDelegate = self;
searchResultsDelegate is the delegate for the table view in which the search results are displayed, and didSelectRowAtIndexPath is one of the table view delegate methods.

Resources