iosuitableviewuisearchresultscontroller

UISearchbarController is returning a wrong indexpath?


Im working on a tableview with a searchBardisplayController but when I type a search this shows the correct results but when I select the cell shows me a different selection (such as if I select a cell when the tableview is normal, the normal indexpath)

So heres my code:

number of rows

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // NSLog(@"%i", [[InfoWebDoctores sharedInstance]numberOfInfo]);

    if (tableView == self.searchDisplayController.searchResultsTableView) {
        return [displayObject count];
    } else  {
        return [allObject count];
    }

}

Configure the cell:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"docCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if(!cell)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"docCell"];
    }



    return cell;
}

Make the search:

- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
    if([searchString length] == 0)
    {
        [displayObject removeAllObjects];
        [displayObject addObjectsFromArray:allObject];
    }
    else
    {
        [displayObject removeAllObjects];
        for(NSDictionary *tmpDict in allObject)
        {
            NSString *val = [tmpDict objectForKey:doctorname];
            NSRange r = [val rangeOfString:searchString options:NSCaseInsensitiveSearch];

            NSString *val2 = [tmpDict objectForKey:doctoresp];
            NSRange r2 = [val2 rangeOfString:searchString options:NSCaseInsensitiveSearch];


            if(r2.location != NSNotFound || r.location != NSNotFound)
            {
                [displayObject addObject:tmpDict];
            }
        }
    }
    return YES;
}

And finally sending the data to the detailViewcontroller (Ive tryied with push connection but when I select a searchresult Cell not even get the push view...)

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    DetailTableViewController *detail = [self.storyboard instantiateViewControllerWithIdentifier:@"detailController"];


    NSIndexPath *index = [self.searchDisplayController.searchResultsTableView indexPathForSelectedRow];
    //NSIndexPath *index = [self.tableView indexPathForSelectedRow];
    int row = [index row];

    EntryJsonDoctores *entry = [[InfoWebDoctores sharedInstance]entryAtIndex:row];

    detail.modal = @[entry.nombre,entry.especialidad, entry.especialidad2, entry.especialidad3, entry.cel, entry.mail, entry.tel1, entry.tel2, entry.ext,entry.hospital,entry.direccion];


    [self presentViewController:detail animated:YES completion:^{

    }];
}

So Ive tryied with the indexpathforselectedrow from the tableview and the indexpath from searchresultscontroller, whats wrong? I need someones help please


Solution

  • If you set the search display controller's resultsTableView's delegate to this View Controller, then the results TableView will be looking for the didSelectRowAtIndexPath.

    You can do this in the viewDidLoad and put self.searchDisplayController.searchResultsTableView.delegate = self

    Replace NSIndexPath *index = [self.searchDisplayController.searchResultsTableView indexPathForSelectedRow]; with NSIndexPath *index = indexPath;