Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UISearchBar textDidChange creating error: There are visible views left after reusing them all: { (null) = (null); }

I'm using a UITableViewController with a UISearchBar. Everything seems to work fine, except I'm getting a strange warning in the textDidChange method that I've never seen before.

This is my code:

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

    [self.searchResults removeAllObjects];
    if([searchText isEqualToString:@""]||searchText==nil){
        [self.tableView reloadData];
        return;
    }

    for(NSArray *monsterArray in self.monsterArray) {
        NSString *name = monsterArray[0];
        NSRange r = [[name lowercaseString] rangeOfString:[searchText lowercaseString]];
        if(r.location != NSNotFound) {
            if(r.location==0) {
                [self.searchResults addObject:monsterArray];
            }
        }
    }

    [self.tableView reloadData];
}

By stepping through the program, I've found that the warning occurs right before the end of textDidChange. As I mentioned in the title, the warning is this:

There are visible views left after reusing them all: { (null) = (null); }

Does anyone know why this is happening, and how to resolve it?

like image 366
Charles Avatar asked Nov 14 '15 01:11

Charles


2 Answers

I had a similar issue with a section header view with a custom UITextField. I got rid of the warning by calling resignFirstResponder on the text field before reloading the table view data, and calling becomeFirstResponder after the reload operation. Something like:

// Workaround: hide and show keyboard to prevent warning when reloading results
[self.searchTextField resignFirstResponder];
[self.tableView reloadData];
[self.searchTextField becomeFirstResponder];
like image 95
Matteo Lallone Avatar answered Oct 31 '22 06:10

Matteo Lallone


While the other answer did get rid of the error being put in the console it also had some unintended consequences. Mainly calling resignFirstResponder and then becomeFirstResponder like that resets the state of the keyboard. So if you type a letter, the keyboard resets to Alpha from Numeric. This becomes annoying if you're trying to type a string of letters.

In my case I found the There are visible views left after reusing them all: { (null) = (null); } error was only logged when I had my UISearchBar set to the TableView's section header. I was doing this to keep the search bar floating at top of a UITableViewController.

Instead I refactored to use a UIViewController, placed the UISearchBar at the top and the UITableView under it this seems to have properly fixed the problem.

like image 41
Kris Avatar answered Oct 31 '22 04:10

Kris