2012-02-17 7 views
5

को segue के लिए तो, स्टोरीबोर्ड का उपयोग कर आप UITableViewCell से एक segue एक detailViewController करने के लिए पहले tableViewController से बना सकते हैं।कैसे एक UISearchBarDisplayController परिणाम से किसी detailViewController

बहुत जटिल नहीं है, हालांकि, जब एक UISearchBarDisplayController स्टोरीबोर्ड मिश्रण में पेश किया जाता है, तो आप परिणाम सेल को विस्तार से क्यू कंट्रोलर पर कैसे लगा सकते हैं? http://clingingtoideas.blogspot.com/2010/02/uitableview-how-to-part-2-search.html

सभी मैं क्या कर सकते हैं खोज से एक पंक्ति का चयन है, यह नीला हो जाता है और detailViewController करने के लिए जाना नहीं करता है:

मैं एक समस्या के बिना खोज करने में सक्षम हूँ, मैं इस ट्यूटोरियल का पालन किया।

मैंने विधि तैयार करने के लिए लागू किया है ForSegue, जो गैर खोजी गई कोशिकाओं के लिए काम करता है, लेकिन यह पता नहीं लगा सकता है।

+0

/आपने कैसे तैयार किया है 'readyForSeque: प्रेषक:'? मैं कैसे के बारे में searchDisplayController.active वर्तमान tableview तय करने के लिए जाँच http://stackoverflow.com/questions/10033279/prepareforsegue-after-uisearchdisplaycontroller/19814031#19814031 – hop

उत्तर

0

ठीक है, मैं मुझे मिल गया लगता है, यह एक हैक का एक सा की तरह लगता है लेकिन यह मेरी प्रयोजनों के लिए काम करता है:

मैं स्टोरीबोर्ड उपयोग कर रहा हूँ: मैं सीधे यह की चोटी पर UISearchBarDisplayController के साथ एक UITableView नियंत्रक की है। कोई कोड बस खींचें और छोड़ें। केवल मुझे मूल सरणी से एक सेल प्रदर्शित करते हैं, खोज सरणी के साथ नहीं दिया जाएगा:

वहाँ से, मैं इस ट्यूटोरियल खोज करने के लिए खोज पट्टी सही ढंग से http://clingingtoideas.blogspot.com/2010/02/uitableview-how-to-part-2-search.html

हालांकि prepareForSegue प्राप्त करने के लिए पीछा किया।

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

searchRowSelected किसी पूर्णांक चर है कि मैं वर्ग के शीर्ष पर घोषित है:

तो मैं didSelectRowAtIndexPath इस्तेमाल किया।

didSelectRowAtIndexPath: पता था कि मैं कौन सी पंक्ति का चयन कर रहा था, लेकिन तैयार करें ForSegue नहीं था। Thats क्यों मुझे उस चर की जरूरत है।

यह वह जगह है मैं यह कैसे 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

में एक साधारण कार्यान्वयन का वर्णन किया है? –

2

मैंने आपके समाधान की कोशिश की और पाया कि तैयार क्षेत्र जीवन चक्र के कारण दो बार कहा जाता है और चयन किया गया है ... -> performSegueWithIdentifier।

  1. स्वयं: prepareForSegue: गंतव्य नियंत्रक पर वस्तु सेट कर दिया जाता (गलत सूचकांक के साथ), क्योंकि
  2. गंतव्य: viewDidLoad: गंतव्य नियंत्रक दृश्य भरी हुई है जो
  3. स्वयं के बाद: didSelectRow ...: सूचकांक ज्ञात और सही ढंग से सेट है।
  4. स्वयं: readyForSegue: ऑब्जेक्ट अब सही है लेकिन इसका कोई दुष्प्रभाव नहीं है।

मैंने तब पर ध्यान केंद्रित किया ...

- (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 
} 
और इस समाधान जहाँ मैं segue नष्ट कर दिया और प्रोग्राम के रूप में देखने के लिए धक्का दिया के साथ आया था

जो तरीका यह करने के लिए नहीं है, लेकिन अब के लिए यह काम करता है।

हालांकि, पहले भाग के लिए आसान और साफ है, और शायद यह भी आप के लिए काम करता है ?!

4

यहाँ समाधान है कि @James चेन द्वारा टिप्पणी के आधार पर किया है। तालिका के अंदर किस राज्य के आधार पर एक अलग इंडेक्सपैथ का उपयोग करना है।

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

    } 
} 
संबंधित मुद्दे