2012-02-17 14 views
5

Vì vậy, sử dụng bảng phân cảnh bạn có thể tạo ra một phân đoạn từ UITableViewCell từ tableViewController đầu tiên đến một detailViewController.Làm thế nào để phân biệt kết quả UISearchBarDisplayController thành một detailViewController

Không quá phức tạp, tuy nhiên, khi một UISearchBarDisplayController được đưa vào hỗn hợp bảng phân cảnh, cách bạn có thể phân tách ô kết quả thành detailViewController?

tôi có thể tìm kiếm mà không có một vấn đề, tôi theo hướng dẫn này: http://clingingtoideas.blogspot.com/2010/02/uitableview-how-to-part-2-search.html

Tất cả tôi có thể làm là chọn một hàng từ tìm kiếm, nó chuyển thành màu xanh và không đi đến detailViewController.

Tôi đã triển khai phương thức prepareForSegue, hoạt động cho các ô không được tìm kiếm, nhưng không thể tìm ra ô này.

+0

/Bạn/bạn đã thực hiện 'preparForSeque: sender:'? Tôi đã mô tả cách triển khai đơn giản trong http://stackoverflow.com/questions/10033279/prepareforsegue-after-uisearchdisplaycontroller/19814031#19814031 – hop

Trả lời

0

Ok Tôi nghĩ rằng tôi đã nhận nó, nó có vẻ như một chút của một hack nhưng nó hoạt động vì mục đích của tôi:

Tôi đang sử dụng kịch bản: Tôi có một bộ điều khiển UITableView với UISearchBarDisplayController trực tiếp trên đầu trang của nó. Không có mã nào chỉ cần kéo và thả.

Từ đó, tôi theo hướng dẫn này để có được thanh tìm kiếm để tìm kiếm chính xác http://clingingtoideas.blogspot.com/2010/02/uitableview-how-to-part-2-search.html

Tuy nhiên prepareForSegue: sẽ chỉ cho tôi hiển thị một tế bào từ các mảng ban đầu, không phải với mảng tìm kiếm.

Vì vậy, tôi sử dụng didSelectRowAtIndexPath:

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

searchRowSelected là một biến int mà tôi tuyên bố ở phía trên cùng của lớp.

didSelectRowAtIndexPath: biết hàng nào tôi đã chọn, nhưng PrepareForSegue thì không. Đó là lý do tại sao tôi cần biến đó.

Đây là cách tôi đã sử dụng nó trong 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; 
    } 
} 
} 

Cũng sử dụng mã này để làm sạch savedSearchTerm

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

Nếu ai đã có một giải pháp tốt hơn tôi là tất cả tai :)

+5

Cách kiểm tra searchDisplayController.active để quyết định xem bảng hiện tại? –

2

Tôi đã thử giải pháp của bạn và thấy rằng preparForSegue được gọi hai lần do vòng đời và didSelect ... -> performSegueWithIdentifier.

  1. tự: prepareForSegue: đối tượng trên điều khiển đích được thiết lập (với chỉ mục sai) vì
  2. dest: viewDidLoad: quan điểm điều khiển đích được nạp sau đó
  3. tự: didSelectRow ...: chỉ số được biết và đặt đúng.
  4. self: prepareForSegue: đối tượng đã chính xác nhưng không có tác dụng phụ.

Sau đó tôi tập trung vào didSelect ...và đã đưa ra giải pháp này, nơi tôi đã xóa các segue và đẩy quan điểm lập trình:

- (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) 
} 

sau đó tôi đã trải qua một số vấn đề sẽ trở lại bởi vì tôi làm theo một segue từ một MapView, dẫn đến:

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

không phải là cách để làm điều đó nhưng bây giờ nó hoạt động.

Tuy nhiên, phần đầu tiên dễ dàng và sạch sẽ, và có thể nó cũng phù hợp với bạn ?!

4

Đây là giải pháp dựa trên nhận xét của @James Chen. Cũng sử dụng một IndexPath khác tùy thuộc vào trạng thái của bảng.

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

    } 
} 
Các vấn đề liên quan