0

I've created a TableView Controller that contains custom cells, and is populated with data from a MySQL database. Everything works great.

I added a search bar, and the search also works great. However, when I select the search results, it brings me to the wrong detail view (e.g. I click on "Cookies" and it sends me to the detail view for "Apples"). Am I missing something in my .m file? Here's my code:

.m

- (int)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    if (tableView == self.searchDisplayController.searchResultsTableView) {
        return [searchResults count];



    } else {
        return [Strains count];

    }


}


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

    StrainTableCell *cell = (StrainTableCell *)[tableView dequeueReusableCellWithIdentifier:strainTableIdentifier];
    if (cell == nil) 


        cell = [[StrainTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:strainTableIdentifier];



        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"StrainTableCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];

    if (tableView == self.searchDisplayController.searchResultsTableView) {

        cell.titleLabel.text = [[searchResults objectAtIndex:indexPath.row] objectForKey:@"Title"];
        cell.descriptionLabel.text = [[searchResults objectAtIndex:indexPath.row] objectForKey:@"Description"];
        cell.ratingLabel.text = [[searchResults objectAtIndex:indexPath.row] objectForKey:@"Rating"];


        NSLog(@"%@", searchResults);
    } else {
        cell.titleLabel.text = [[Strains objectAtIndex:indexPath.row] objectForKey:@"Title"];
         cell.descriptionLabel.text = [[Strains objectAtIndex:indexPath.row] objectForKey:@"Description"];
         cell.ratingLabel.text = [[Strains objectAtIndex:indexPath.row] objectForKey:@"Rating"];


    }

    {

    } 


return cell;

}



- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
    NSPredicate *resultPredicate = [NSPredicate 
                                    predicateWithFormat:@"SELF contains[cd] %@",
                                    searchText];

    searchResults = [Strains filteredArrayUsingPredicate:resultPredicate];


}

-(BOOL)searchDisplayController:(UISearchDisplayController *)controller 
shouldReloadTableForSearchString:(NSString *)searchString
{
    [self filterContentForSearchText:searchString 
                               scope:[[self.searchDisplayController.searchBar scopeButtonTitles]
                                      objectAtIndex:[self.searchDisplayController.searchBar
                                                     selectedScopeButtonIndex]]];

    return YES;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    StrainDetailViewController *detailViewController = [[StrainDetailViewController alloc]
                                                        initWithNibName:@"StrainDetailViewController" bundle:nil];
    detailViewController.title = [[Strains objectAtIndex:indexPath.row] objectForKey:@"Title"];
    detailViewController.strainDetail = [Strains objectAtIndex:indexPath.row];
              [self.navigationController pushViewController:detailViewController animated:YES];


    }

1 Answers1

0
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
StrainDetailViewController *detailViewController = [[StrainDetailViewController alloc]                                                 initWithNibName:@"StrainDetailViewController" bundle:nil];
if (self.searchDisplayController.searchResultsTableView) {

      detailViewController.title = [[searchResults objectAtIndex:indexPath.row] objectForKey:@"Title"];
      detailViewController.strainDetail = [searchResults objectAtIndex:indexPath.row];

} else {

     detailViewController.title = [[Strains objectAtIndex:indexPath.row] objectForKey:@"Title"];
     detailViewController.strainDetail = [Strains objectAtIndex:indexPath.row];
}

[self.navigationController pushViewController:detailViewController animated:YES];
}
Adrian P
  • 6,479
  • 4
  • 38
  • 55
spet
  • 51
  • 3
  • Hi! This fixes my search bar results in terms of showing the right detail view controller. However when I implement this, it removes the data from my Detail View Controller when I'm not searching. E.g. If I simply make a selection from my table view, it pushes to an empty detail view. – Blair Warner Apr 08 '13 at 16:32
  • empty cause by [Strains objectAtIndex:indexPath.row] return empty. I suggest you should not use two UITableView (instance). you can change the dataSoure of UITableView and reload UITableView if you are searching. – spet Apr 10 '13 at 03:28
  • I don't know how the two UITableView display. the same frame? – spet Apr 10 '13 at 03:39
  • "...you can change the data source of UITableView and reload UITableView if you are searching." How would I go about doing this? I'm confused (and fairly new at this). – Blair Warner May 07 '13 at 17:09