-3

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 :)

Community
  • 1
  • 1
  • I worked on this for a long time and finally decided it would be easier to just place the search bar in a view right above the table. With autolayout it is easy to control the positioning and getting the header to positioned in the same place at all times, IIRC, was the problem. I gave up on it. Good luck in finding a workaround (I think I was on ios7 at that time). – RegularExpression Jan 08 '16 at 06:02

1 Answers1

0

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 :))