0

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

Karlo A. López
  • 2,548
  • 3
  • 29
  • 56

1 Answers1

0

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;

Eytan Schulman
  • 566
  • 2
  • 4
  • 20
  • Thanks for your answer, all the delegates are connected in the storyboard, I`ve tried what you propose but It continue the same problem,thanks – Karlo A. López Aug 03 '14 at 15:11
  • @user3290977 Well there is no point in doing indexPathForSelectedRow because you are already in a method that is supplying the selectedRow. indexPathForSelectedRow is used when you are outside of didSelectRowAtIndexPath. Also, if you are in storyboard you really should use Segue's instead of instantiating the view controller. – Eytan Schulman Aug 03 '14 at 15:13
  • You are right, I miss that because I have that code in prepareforsegua and I didnt change It, but when I make the search even with that indexpath from the tableview I get a wrong selecion (only on search) – Karlo A. López Aug 03 '14 at 15:14