Rushang Prajapati Rushang Prajapati - 5 months ago 59
Objective-C Question

Custom Searchbar in iOS

Hello i am using

UISerchbar
in
tableview
but is there any easy way to create custom
searchbar
in
tableview headerview
?

i already check below link but i am not understand.

Custom UISearchBar with UISearchController

How do I use the UISearchBar and UISearchDisplayController

I have added searchDisplayController : :
enter image description here

And This is my code for searchController,

self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
self.searchController.searchResultsUpdater = self;
self.searchController.dimsBackgroundDuringPresentation = NO;
self.searchController.searchBar.delegate = self;
self.definesPresentationContext = YES;
[self.searchController.searchBar setBarTintColor:[UIColor darkGrayColor]];
self.tableView.tableHeaderView = self.searchController.searchBar;


I have set delegate for
UISearchBarDelegate,UISearchResultsUpdating
as well,

and implemented their method ,

- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
if (_searchController.isActive && _searchController.searchBar.text.length >0){

NSPredicate *resultPredicate = [NSPredicate
predicateWithFormat:@"SELF.name contains[cd] %@",
searchText];

abcd = [sortedEventArray filteredArrayUsingPredicate:resultPredicate];
NSLog(@"%@",searchResults);

}


}

- (void)updateSearchResultsForSearchController:(UISearchController *)searchController1
{
NSString *searchString = searchController1.searchBar.text;
[self filterContentForSearchText:searchString scope:@"abc"];
//[self filterContentForSearchText:searchString :@"abc"];
[self.tableView reloadData];
}


cancel button event

- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
abcd=[sortedEventArray mutableCopy];
[_tableView reloadData];

}



-(void)delete{


NSManagedObjectContext *context = [self managedObjectContext];
NSFetchRequest * allMovies = [[NSFetchRequest alloc] init];
[allMovies setEntity:[NSEntityDescription entityForName:@"Data" inManagedObjectContext:context]];
[allMovies setIncludesPropertyValues:NO]; //only fetch the managedObjectID

NSError * error = nil;
NSArray * movies = [context executeFetchRequest:allMovies error:&error];
//error handling goes here
for (NSManagedObject * movie in movies) {
[context deleteObject:movie];
}
NSError *saveError = nil;
[context save:&saveError];
}


Still Its not working

Thanks in advance :)

Answer

There is everythings is complete but there is two little mistakes by me.

1) delete the search bar from Storyboard.

2) Add [self.searchController.searchBar sizeToFit]; in viewDidLoad

Thanks :))

Comments