如何从UISearchBarDisplayController结果转换为detailViewController

M J*_*sse 5 uisearchbardisplaycontrol ios

因此,使用storyboard,您可以从UITableViewCell创建一个segue,从第一个tableViewController到detailViewController.

但是,当故事板混合中引入UISearchBarDisplayController时,不是太复杂,如何将结果单元格转换为detailViewController?

我能够毫无问题地搜索,我遵循了这个教程:http://clingingtoideas.blogspot.com/2010/02/uitableview-how-to-part-2-search.html

我所能做的就是从搜索中选择一行,它变为蓝色,不会转到detailViewController.

我已经实现了prepareForSegue方法,该方法适用于未搜索的单元格,但无法弄清楚这一点.

M J*_*sse 0

好吧,我想我明白了,这看起来有点像黑客,但它适合我的目的:

我正在使用情节提要:我有一个 UITableview 控制器,其上直接有 UISearchBarDisplayController。无需代码,只需拖放即可。

从那里,我按照本教程让搜索栏正确搜索http://clingingtoideas.blogspot.com/2010/02/uitableview-how-to-part-2-search.html

但是,prepareForSegue: 只会让我显示原始数组中的单元格,而不是搜索数组中的单元格。

所以我使用了didSelectRowAtIndexPath:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (savedSearchTerm) {
    searchRowSelected = indexPath.row;  //<-- the trick that worked
    [self performSegueWithIdentifier:@"ShowDetail" sender:self];
}
}
Run Code Online (Sandbox Code Playgroud)

searchRowSelected 是我在类顶部声明的 int 变量。

didSelectRowAtIndexPath:知道我正在选择哪一行,但prepareForSegue不知道。这就是为什么我需要这个变量。

这就是我在prepareForSegue中使用它的方式:

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if ([segue.identifier isEqualToString:@"ShowDetail"]) {

    dvc = [segue destinationViewController];

    NSIndexPath* path = [self.tableView indexPathForSelectedRow];
    int row = [path row];

    if (savedSearchTerm){   //set the detailViewController with the searched data cell 
        myDataClass* c = [searchResults objectAtIndex:searchRowSelected];
        dvc.myDataClass = c;
    }else{    //set the detailViewController with the original data cell
       myDataClass* c = [array objectAtIndex:row];
       dvc.myDataClass = c;
   }
}
}
Run Code Online (Sandbox Code Playgroud)

还可以使用此代码来清理savedSearchTerm

-(void) searchDisplayControllerWillEndSearch:(UISearchDisplayController *)controller{
    [self setSavedSearchTerm:nil];
}
Run Code Online (Sandbox Code Playgroud)

如果有人有更好的解决方案,我会洗耳恭听:)

  • 如何检查 searchDisplayController.active 来决定当前的表视图? (5认同)