2013-02-19 3 views
0

저는 작은 프로젝트를 진행하고 있습니다. 문제가 있습니다. UISearcBar있는 UITableView 있습니다. 모든 것이 잘 작동하고 검색 결과가 정확하지만 이제는 각 검색 결과에 대한 detailViewController로 이동하기 위해 prepareForSegue 메서드를 사용하려고합니다.prepareForSegue를 사용하는 UISearchBar

예 : 제품 "A"를 검색하여 찾으면 해당 제품을 선택하면 ViewController_A로 이동하고, 제품 "B"를 검색하고 선택하면 ViewControler_B로 이동해야합니다.

이 코드를 사용하면 내가 선택한 것을 mather로 만들지 않고 동일한 ViewController로 이동합니다.

#pragma mark - TableView Delegate 
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
    // Perform segue to candy detail 
    [self performSegueWithIdentifier:@"candyDetail" sender:tableView]; 


} 

#pragma mark - Segue 
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { 
    if ([[segue identifier] isEqualToString:@"candyDetail"]) { 
     UIViewController *candyDetailViewController = [segue destinationViewController]; 



     // In order to manipulate the destination view controller, another check on which table (search or normal) is displayed is needed 
     if(sender == self.searchDisplayController.searchResultsTableView) { 
      NSIndexPath *indexPath = [self.searchDisplayController.searchResultsTableView indexPathForSelectedRow]; 
      NSString *destinationTitle = [[filteredCandyArray objectAtIndex:[indexPath row]] name]; 
      [candyDetailViewController setTitle:destinationTitle]; 
     } 
     else { 
      NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow]; 
      NSString *destinationTitle = [[candyArray objectAtIndex:[indexPath row]] name]; 
      [candyDetailViewController setTitle:destinationTitle]; 
     } 

    } 
     } 

답변

0

항상 동일한 segueId 인 "candyDetail"을 호출하기 때문입니다.

대신에, 당신이 준비해 놓은이 매뉴얼 segues가 있어야 당신의 UIStoryBoard, 다른 장면에 각 포인팅 (ViewControllerB 가리키는 "showViewControllerA"ViewControllerA로 가서 다른 "showViewControllerB"의 ID로 하나). 그런 다음 다음을 수행 할 수 있습니다

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if ([[self.candyArray objectAtIndex:indexPath.row] isKindOfClass:[CandyA class]]) { 
     [self performSegueWithIdentifier:@"showViewControllerA" sender:self]; 
    } else if ([[self.candyArray objectAtIndex:indexPath.row] isKindOfClass:[CandyB class]]) { 
     [self performSegueWithIdentifier:@"showViewControllerB" sender:self]; 
    }; 
} 

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
{ 
    if ([[segue identifier] isEqualToString:@"showViewControllerA"]) { 
     ViewControllerA *viewControllerA = [segue destinationViewController]; 
     // configure viewControllerA here... 
    } else if ([[segue identifier] isEqualToString:@"showViewControllerA"]) { 
     ViewControllerB *viewControllerB = [segue destinationViewController]; 
     // configure viewControllerB here... 
    } 
} 

또 다른 대안은 당신이 조치가 다른 세포에 직접 segues 철사, 당신이 당신의 소스 배열에있는 사탕 유형에 따라 -tableView:cellForRowAtIndexPath:에 큐에서 셀 유형을 전환 할 수 있습니다. 어느 쪽이든, 서로 다른 장면을 가리키는 두 개의 segue가 필요합니다.

+0

감사합니다. 그리고 N 항목이있는 경우 ViewController의 권한을 N 만들 필요가 있습니까? 왜냐하면 저는 두 가지 아이템만을 사용하여 예제를 제공하기 때문에이 프로젝트에서는 아마도 20 개의 아이템이있을 것입니다. –

+0

N 개의 항목에 대해 N 개의보기 컨트롤러가 필요하지 않습니다. 한 개의 대상으로 20 개의 항목을 모두 처리 할 수 ​​있습니다. 원래 질문은 선택한 행을 기반으로 _different_ 대상보기 컨트롤러 클래스를 시작하는 방법을 묻습니다. 설정하려는 _same_ 대상보기 컨트롤러 클래스 (예 : 제목 및 기타 공개 속성)의 속성 일 경우 원래 코드는 정상적으로 작동합니다. – followben

+0

감사. 그러나 잘 이해하지 못합니다. 나는 이름, 크기, 형식, 날짜와 같은 다른 특징들을 가진 20 개의 itens를 가지고있다. 이 정보는 정적이며 각 캔디에 대한 ViewController에 있습니다. 따라서 Candy1을 선택하면 prepareForSegue가 VC1으로 이동하고 Candy 12를 선택하면 VC12로 이동해야합니다. 그래서 나는 TableViewController에서 선택한 사탕과 세부 VC 사이의 어딘가를 만들어야합니다. –

관련 문제