Search code examples
iosobjective-cipaduitableviewuisearchdisplaycontroller

iPad search display controller displaying results starting in second cell


I added a Searchbar and Search Display Controller to my iPad app that use localSearch. I believe I implemented the delegates correctly. The search works fine and displays the results but the issues is that these results start in the second cell. The first cell in the popup display being empty. I double checked to see if the map items count and contents were correct and they were.

The code:

- (void) searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    if (![self connected]) {
        [[[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Cannot Connect",nil)
                                    message:NSLocalizedString(@"Please make sure you are connected to the internet and try again.",nil)
                                   delegate:nil
                          cancelButtonTitle:NSLocalizedString(@"OK",nil) otherButtonTitles:nil] show];
    }
    else if ([mainToolbar isHidden])
    {
        [[[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Search Unavailable",nil)
                                    message:NSLocalizedString(@"Please make sure you aren't drawing an AOI or using a tool and then try again.",nil)
                                   delegate:nil
                          cancelButtonTitle:NSLocalizedString(@"OK",nil) otherButtonTitles:nil] show];
    }
    else {
        // Cancel any previous searches.
        [localSearch cancel];

        // Perform a new search.
        MKLocalSearchRequest *request = [[MKLocalSearchRequest alloc] init];
        request.naturalLanguageQuery = self.searchBar.text;
        request.region = self.mapView.region;

        [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
        localSearch = [[MKLocalSearch alloc] initWithRequest:request];

        [localSearch startWithCompletionHandler: ^(MKLocalSearchResponse *response, NSError *error){
            [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;

            if ([response.mapItems count] == 0)
            {
                [[[UIAlertView alloc] initWithTitle:NSLocalizedString(@"No Results",nil)
                                            message:nil
                                           delegate:nil
                                  cancelButtonTitle:NSLocalizedString(@"OK",nil) otherButtonTitles:nil] show];
                return;
            }

            if (error != nil)
            {
                [[[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Map Error",nil)
                                            message:NSLocalizedString(@"Sorry.", nil)
                                           delegate:nil
                                  cancelButtonTitle:NSLocalizedString(@"OK",nil) otherButtonTitles:nil] show];
                return;
            }

            results = response;

            [self.searchDisplayController.searchResultsTableView reloadData];
        }


         ];
    }
}


- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [results.mapItems count];
}


- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *IDENTIFIER = @"SearchResultsCell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:IDENTIFIER];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:IDENTIFIER];
    }

    MKMapItem *item = results.mapItems[indexPath.row];

    cell.textLabel.text = item.name;
    cell.detailTextLabel.text = item.placemark.addressDictionary[@"Street"];

    return cell;
}

Here is a screenshot of the issue.

Search display skipping the first cell


Solution

  • The first row contains the table header.