0

我创建了一个包含自定义单元格的 TableView 控制器,并填充了来自 MySQL 数据库的数据。一切都很好。

我添加了一个搜索栏,搜索效果也很好。但是,当我选择搜索结果时,它会将我带到错误的详细视图(例如,我单击“Cookies”并将我发送到“Apples”的详细视图)。我的 .m 文件中是否缺少某些内容?这是我的代码:

.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];


    }
4

1 回答 1

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];
}
于 2013-04-08T07:50:31.577 回答