Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

UITableView Search Bar - Cancel Button Shows Blank Table/Not Clearing Results

ariearie Posts: 29New Users @
Hi guys

I've searched for some solutions here but didn't seem to find anything. Could anyone kindly advise?

I have a SearchDisplay Controller and a Search Bar on a Table View. The search works great, so does the Segue from the Table View to the details View Controller when a cell is tapped, apart from the following:

If text is entered into the Search Bar, and then the cancel button is pressed, the TableView does not display ALL results instead of the filtered results. The only way to show all (unfiltered) results again is to tap the search bar, enter some text again and then backspace until the search bar in blank.

I have the following SearchBar delegate code:
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
[searchBar setShowsCancelButton:NO animated:YES];
searchBar.text = @"";
NSLog(@"Searchbar text set from cancel to: %@ with length %i",searchBar.text,searchBar.text.length);
NSLog(@"Cancel Clicked");

[self.tableView reloadData];
[searchBar resignFirstResponder];


The code for the SearchBar textdidchange delegate:

-(void)searchBar:(UISearchBar*)searchBar textDidChange:(NSString*)text
{
if(text.length == 0 )
{
NSLog(@"length = 0");
self.isFiltered = false;
}
else
{
self.isFiltered = true;
self.filteredTableData = [[NSMutableArray alloc] init];

//Search for searchBar text in array
int i;
for (i = 0; i < [self.allResultsArray count]; i++)
{
// NSLog(@"Count value = %i",[self.allResultsArray count]);
id myArrayElement = [self.allResultsArray objectAtIndex:i];
//NSLog(@"Current Object in loop = %@",myArrayElement);
NSString *tempName = [myArrayElement objectForKey:@"name"];
// NSLog(@"NAME = %@",tempName);
NSRange nameRange = [tempName rangeOfString:text options:NSCaseInsensitiveSearch];

if(nameRange.location != NSNotFound)
{
[self.filteredTableData addObject:myArrayElement];
NSLog(@"Entered search text =%@",text);
NSLog(@"Filtered Table Data = %@",self.filteredTableData);
}
}
}

[self.tableView reloadData];

}


Any advice would be much appreciated.

Thanks

Replies

  • smithdale87smithdale87 Posts: 4,447iPhone Dev SDK Supporter @ @ @ @ @
    show your tableview delegate methods (numberOfRowsInSection, cellForRowAtIndexPath)
  • BrianSlickBrianSlick Treadmill Desk Ninja Posts: 10,677Tutorial Authors, Registered Users @ @ @ @ @ @ @ @
    And see the table view link in my signature.
    Professional iOS App Development. Available for hire.
    BriTer Ideas LLC - WWW | Facebook | Twitter | LinkedIn

    BTIKit | BTICoreDataKit | SlickShopper 2 | Leave a PayPal donation
  • ariearie Posts: 29New Users @
    Doh! Simple as adding the following code in the searchBarCancelButtonClicked delegate :D

    self.isFiltered = false;

    :D Thanks peeps! @BrianSlick - some great info in your tuts, thank you.
  • BrianSlickBrianSlick Treadmill Desk Ninja Posts: 10,677Tutorial Authors, Registered Users @ @ @ @ @ @ @ @
    Voice of experience: Don't go with an 'isFiltered' approach. Use different arrays. Make decisions based on which table is asking.
    Professional iOS App Development. Available for hire.
    BriTer Ideas LLC - WWW | Facebook | Twitter | LinkedIn

    BTIKit | BTICoreDataKit | SlickShopper 2 | Leave a PayPal donation
  • ariearie Posts: 29New Users @
    Stepping stones....stepping stones... :D But I shall definitely go over your tutorial and try out that method! :D :-bd
  • BrianSlickBrianSlick Treadmill Desk Ninja Posts: 10,677Tutorial Authors, Registered Users @ @ @ @ @ @ @ @
    What happens is that even though you are 'isFiltering', that doesn't mean the main table view is done asking questions. So you answer the question as though it is for the search table, but in reality the main table is the one asking, so you give the wrong answer, and very often you crash.
    Professional iOS App Development. Available for hire.
    BriTer Ideas LLC - WWW | Facebook | Twitter | LinkedIn

    BTIKit | BTICoreDataKit | SlickShopper 2 | Leave a PayPal donation
  • ariearie Posts: 29New Users @
    Hmm gonna read that a few more times and try to understand what you mean. Do you perhaps have an example for how it could crash with the current isFiltered variable?
  • BrianSlickBrianSlick Treadmill Desk Ninja Posts: 10,677Tutorial Authors, Registered Users @ @ @ @ @ @ @ @
    The - (UITableView *)tableView parameter of all of these methods is the table view that is asking the question. How many rows in this section? The main table view will ask. The search table view will also ask. This method has to answer the question for both.

    So what you're thinking is that it goes like this:
    isFiltering = NO;
    return 500;
    isFiltering = YES;
    return 2;
    But what is really happening is this:
    Main table view: how many rows?
    Search table view: how many rows?
    Those will be different answers, and it depends on which table is asking the question. So what can actually happen is:
    Main table view: how many rows?
    if (isFiltering)
    return 2;
    else
    return 500;

    Search table view: how many rows?
    if (isFiltering)
    return 2;
    else
    return 500;
    You have the opportunity for one wrong answer either way. You could give the filtered answer to the main table view, or the non-filtered answer to the search table view. Depending on the situation, the wrong answer results in crashes.

    Just because you have set isFiltering = YES does _not_ mean that the main table view will not call these methods anymore. The only time the use of this technique is appropriate is if there is only a single table view. But that's not the case here, you have 2 table views.
    Professional iOS App Development. Available for hire.
    BriTer Ideas LLC - WWW | Facebook | Twitter | LinkedIn

    BTIKit | BTICoreDataKit | SlickShopper 2 | Leave a PayPal donation
Sign In or Register to comment.