1

I am looking to create a searchBar and while I have it somewhat working on my Table View, there's still a bit of effort required to get it 100% perfect.

With reference to iOS 7 Mail.app, how do I deploy something like that? So a search bar that does not display the "cancel" button till you click in the search bar, and a cancel button that cancels the search and returns the table to where it was.

I have the following code:

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    _fetchedResultsController = nil;
    NSError *error;

    if (![[self fetchedResultsController] performFetch:&error])
    {
        NSLog(@"Error in search %@, %@", error, [error userInfo]);
    }

    else
    {
        [self.timelineTableView reloadData];
        [self.timelineSearchBar resignFirstResponder];
        [self.noResultsLabel setHidden:_fetchedResultsController.fetchedObjects.count > 0];
    }
}

- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
    [self.timelineSearchBar resignFirstResponder];
    self.timelineSearchBar.hidden = YES;
    [self.timelineTableView reloadData];
    [self viewDidLoad];
}

So with this, I basically want the searchBar to be visible all the time in the Table View, and to NOT display the cancel button, till the user starts typing. If they perform a search and it produces results or it doesn't, I want the cancel button to:

  • ResignFirstResponder of the search bar
  • Remove the cancel button
  • Return the Table view to how it was before the search.

Thanks

amitsbajaj
  • 1,304
  • 1
  • 24
  • 59
  • You can use searchbar controller from object library rather than just a searchbar. By default that has what you want. – Adrian P Dec 09 '13 at 13:05
  • You can use this link.and see the sample code. https://developer.apple.com/library/iOs/samplecode/AdvancedTableSearch/Introduction/Intro.html – Manimaran Dec 09 '13 at 13:10
  • Thanks both of you - that accepted answer below helped without me needing to use a searchdiplaycontroller - thanks again for the responses – amitsbajaj Dec 09 '13 at 14:55

1 Answers1

3

Use the following two methods in the UISearchBarDelegate and do something like this:

- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
    [searchBar setShowsCancelButton:YES animated:YES];
}

- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar
{
    [searchBar setShowsCancelButton:NO animated:YES];
}

If you want the search to appear more responsive you can move your posted code to the searchBar:textDidChange: delegate method and then only use searchBarSearchButtonClicked: to do a [searchBar resignFirstResponder]:

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    [searchBar resignFirstResponder];
}

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{

    _fetchedResultsController = nil;
    NSError *error;

    if (![[self fetchedResultsController] performFetch:&error])
    {
        NSLog(@"Error in search %@, %@", error, [error userInfo]);
    }

    else
    {
        [self.timelineTableView reloadData];
        [self.noResultsLabel setHidden:_fetchedResultsController.fetchedObjects.count > 0];
    }
}

Additionally you can use the searchBarCancelButtonClicked: to also resign the first responder from the search bar and then update your table view by calling the delegate searchBar:textDidChange::

- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
    [searchBar setText:@""];
    [self searchBar:searchBar textDidChange:@""];
    [searchBar resignFirstResponder];
}
Zappel
  • 1,612
  • 1
  • 22
  • 37
  • Thank you @Zappel - That is honestly perfect and it works like a charm! I'm going to accept that answer and vote it up. I just have one question remaining - the "clear" in the actual search bar, the little x, is that controllable by a method? because what happens now is I search and it shows me the search result and resignsFirstResponder, if I click on the x, it shows brings up the responder and then I can cancel pressing the Cancel button.. the same thing happens if I press the Cancel - is there anyway, while being resigned, I can just press the x and have that reload the table? Thanks again – amitsbajaj Dec 09 '13 at 14:54