Uitableview 在表视图中放置搜索栏。选择结果会给出错误的详细视图吗?

Uitableview 在表视图中放置搜索栏。选择结果会给出错误的详细视图吗?,uitableview,uisearchdisplaycontroller,xcode4.6,Uitableview,Uisearchdisplaycontroller,Xcode4.6,我已经创建了一个包含自定义单元格的TableView控制器,并使用MySQL数据库中的数据进行填充。一切都很好 我添加了一个搜索栏,搜索效果也很好。但是,当我选择搜索结果时,它会将我带到错误的详细视图(例如,我单击“Cookies”,它会将我发送到“Apples”的详细视图)。我的.m文件中缺少什么吗?这是我的密码: .m - (int)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

我已经创建了一个包含自定义单元格的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];


    }

你好这将修复我的搜索栏结果,以显示正确的详细视图控制器。但是,当我实现此功能时,它会在我不搜索时从我的详细视图控制器中删除数据。例如,如果我只是从我的表视图中进行选择,它会推送到一个空的细节视图。空原因由[trains objectAtIndex:indexPath.row]返回空。我建议您不要使用两个UITableView(实例)。如果正在搜索,您可以更改UITableView的数据源并重新加载UITableView。我不知道这两个UITableView是如何显示的。“同一帧?”…如果正在搜索,您可以更改UITableView的数据源并重新加载UITableView。“我将如何执行此操作?”?我很困惑(这方面我还不太了解)。
- (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];
}