2012-02-17 5 views
5

したがって、ストーリーボードを使用すると、最初のtableViewControllerからdetailViewControllerまでのUITableViewCellからセグを作成できます。UISearchBarDisplayController結果からdetailViewControllerへの分割方法

しかし、UISearchBarDisplayControllerがストーリーボードミックスに導入されても複雑ではないが、どのように結果セルをdetailViewControllerにセグメンテーションできますか?

私は問題なく検索することができるよ、私はこのチュートリアルに従っ:http://clingingtoideas.blogspot.com/2010/02/uitableview-how-to-part-2-search.html

私にできることすべてが検索から行を選択され、それが青に変わり、detailViewControllerに行っていません。

私は、検索されていないセルに対して機能するprepareForSegueメソッドを実装しましたが、これを理解することはできません。

+0

/how /あなたは 'prepareForSeque:sender:'を実装しましたか?私は単純な実装について説明しました。http://stackoverflow.com/questions/10033279/prepareforsegue-after-uisearchdisplaycontroller/19814031#19814031 – hop

答えて

0

は、[OK]を私はそれはハックのビットのように思える、私はそれを得たと思いますが、それは私の目的のために動作します:私は、ストーリーボードを使用しています

: は私が直接その上にUISearchBarDisplayControllerとのUITableViewコントローラを持っています。コードをドラッグ&ドロップするだけではありません。

そこから、私が正しく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]; 
} 

誰もがよりよい解決策を持っている場合、私はすべての耳だ:)

+5

searchDisplayController.activeをチェックして現在のテーブルビューを決定することはどうですか? –

2

あなたの解決策を試したところ、ライフサイクルとdidSelect ...-> performSegueWithIdentifierのためにprepareForSegueが という2回呼び出されていることがわかりました。

  1. 自己:prepareForSegue:のviewDidLoad:didSelectRow ...:インデックス先コントローラビューは
  2. 後自己ロードさDEST
  3. ので先コントローラのオブジェクトが(間違ったインデックスを持つ) に設定されています知られており、適切に設定されています。
  4. self:prepareForSegue:オブジェクトは正しくなりましたが、副作用はありません。

私はdidSelectに焦点を当てました...

- (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からセグエ に従っているので、私はその後、戻っていくつかの問題を経験した:そして私はセグエを削除し、プログラムのビューをプッシュし、この解決策を考え出した

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

これはやり方ではありませんが、現在は動作しています。

しかし、最初の部分は簡単できれいで、おそらくそれもあなたのために働くのですか?

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

    } 
} 
関連する問題