5

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

不太复杂,但是,当 UISearchBarDisplayController 被引入故事板组合时,如何将结果单元格与 detailViewController 相结合?

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

我所能做的就是从搜索中选择一行,它变成蓝色并且不去detailViewController。

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

4

3 回答 3

4

这是基于@James Chen 评论的解决方案。还根据表所处的状态使用不同的 IndexPath。

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{

    if ([[segue identifier] isEqualToString:@"toDetail"]) {

        Person *person = nil;

        if (self.searchDisplayController.active == YES) {
            NSIndexPath *indexPath = indexPath = [self.searchDisplayController.searchResultsTableView indexPathForSelectedRow];
            NSLog(@"segue - section: %d, row: %d", indexPath.section, indexPath.row);

            person = [self.filteredPersonArray objectAtIndex:indexPath.row];
        }
        else {
            NSIndexPath *indexPath = indexPath = [self.tableView indexPathForSelectedRow];
            NSLog(@"segue - section: %d, row: %d", indexPath.section, indexPath.row);

            person = [self.personArray objectAtIndex:indexPath.row];
        }

        [[segue destinationViewController] setPerson:person];   

    }
}
于 2013-06-07T14:30:08.433 回答
2

我尝试了您的解决方案,发现由于生命周期和 didSelect... -> performSegueWithIdentifier,prepareForSegue 被调用了两次。

  1. self:prepareForSegue: 目标控制器上的对象已设置(索引错误),因为
  2. dest:viewDidLoad: 目标控制器视图被加载之后
  3. self:didSelectRow...:索引是已知的并正确设置。
  4. self:prepareForSegue: 对象现在是正确的,但没有副作用。

然后我专注于 didSelect... 并提出了这个解决方案,我删除了 segue 并以编程方式推送视图:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    DestTableViewController *controller = [self.storyboard instantiateViewControllerWithIdentifier:@"DestViewController"];

    CustomObj *custObj = nil;

    if (tableView == self.searchDisplayController.searchResultsTableView) {
        custObj = [filteredListContent objectAtIndex:indexPath.row];
    } else {
        storeToDetail = [self.fetchedResultsController objectAtIndexPath:indexPath];
    }

    controller.custObj = custObj;

    [self.navigationController setNavigationBarHidden:NO];
    [self.navigationController pushViewController:controller animated:YES];
    // presentViewController::animated:completion is always full screen (see problem below)
}

然后我遇到了一些问题,因为我遵循了 mapView 的 segue,这导致:

//DestinationViewController
- (IBAction)back:(id)sender
{
    [self.navigationController popToRootViewControllerAnimated:YES]; // list
    [self.presentingViewController dismissViewControllerAnimated:YES completion:nil]; // map
}

这不是这样做的方法,但现在它可以工作。

但是,第一部分简单而干净,也许它也适合你?!

于 2012-04-06T20:50:50.757 回答
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];
}
}

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;
   }
}
}

也使用此代码来清理 savedSearchTerm

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

如果有人有更好的解决方案,我会全神贯注:)

于 2012-02-17T19:58:13.797 回答