本文介绍了如何从UISearchBarDisplayController结果中删除到detailViewController的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

So, using storyboard you can create a segue from the UITableViewCell from the first tableViewController to a detailViewController.

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

Not too complicated, however, when a UISearchBarDisplayController is introduced into the storyboard mix, how can you segue the results cell to the detailViewController?

我能够毫无问题地搜索,我遵循了这个教程:

I am able to search without a problem, I followed this tutorial: http://clingingtoideas.blogspot.com/2010/02/uitableview-how-to-part-2-search.html

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

All I can do is select a row from the search, it turns blue and doesn't go to the detailViewController.

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

I have implemented the method prepareForSegue, which works for the non searched cells, but can't figure out this one.

推荐答案

好吧我觉得我知道了,这似乎有点像黑客但它适用于我的目的:

Ok I think I got it, it seems like a bit of a hack but it works for my purposes:

我正在使用storyboard:
我有一个UITableview控制器,UISearchBarDisplayController直接在它上面。没有代码只是拖放。

I am using storyboard:I have a UITableview controller with UISearchBarDisplayController directly on top of it. No code just drag and drop.

从那里开始,我按照本教程让搜索栏正确搜索

From there, I followed this tutorial to get the search bar to search correctly http://clingingtoideas.blogspot.com/2010/02/uitableview-how-to-part-2-search.html

然而,prepareForSegue:只允许我显示原始数组中的单元格,而不是搜索数组。

However prepareForSegue: would only let me display a cell from the original array, not with the search array.

所以我使用了didSelectRowAtIndexPath:

So I used didSelectRowAtIndexPath:

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

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

searchRowSelected is an int variable that I declared at the top of the class.

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

didSelectRowAtIndexPath: knew which row I was selecting, but prepareForSegue didn't. Thats why I needed that variable.

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

This is how I used it in 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;
   }
}
}

也可以使用此代码清理up savedSearchTerm

Also use this code to clean up savedSearchTerm

-(void) searchDisplayControllerWillEndSearch:(UISearchDisplayController *)controller{
    [self setSavedSearchTerm:nil];
}

如果有人有更好的解决方案,我会全力以赴:)

If anyone has a better solution I'm all ears :)

这篇关于如何从UISearchBarDisplayController结果中删除到detailViewController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 04:20