2012-07-28 11 views
6

मैं UISearchDisplayController को कार्यान्वित कर रहा हूं और मैं searchResultsTableView को तालिका दृश्य (unfiltered) की सामग्री के साथ लोड करना चाहता हूं - टेक्स्ट दर्ज होने से पहले।सर्चबार सक्रिय होने पर searchResultsTableView कैसे दिखाना है?

यह तब काम करता है जब मैं खोजबार में मान दर्ज करना शुरू करता हूं।

- (void)searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller { 
    self.isSearching = YES; 

    //tell the searchDisplayTableView to show here! 

    [controller.searchBar setShowsCancelButton:YES animated:YES]; 
} 

- (void)searchDisplayControllerDidEndSearch:(UISearchDisplayController *)controller { 
    self.isSearching = NO; 
    [controller.searchBar setShowsCancelButton:NO animated:YES]; 
} 

क्या किसी के पास सही दिशा में कोई संकेतक होगा?

कृपया "ऐसा न करें" या "ऐप्पल ने इस तरह से नियंत्रण को डिजाइन नहीं किया है" के साथ उत्तर न दें। या ...

+0

क्या आपको कोई समाधान मिला है? मुझे भी ठीक यही समस्या है। – nonamelive

+0

क्या किसी ने अभी तक यह कैसे किया है? – Chicken

उत्तर

0

एप्पल के नियंत्रण इस तरह काम करता है। मैंने एक अलग नियंत्रक को लागू करके इसे हल किया (इसे खोज डिस्प्ले नियंत्रक कहते हैं) ऑब्जेक्ट जो एक खोज बार प्रतिनिधि है और डेटा स्रोत के लिए एक अनुमान लागू करता है। इस तरह टेबलव्यू फ़िल्टर 'इनलाइन' फ़िल्टर किया जाता है।

0

आप खोजते समय क्या कर रहा है फ़िल्टर किए गए परिणाम का उपयोग कर अपने तालिका दृश्य को फिर से लोड करने की आवश्यकता है: इस विधि में सिर्फ तालिका दृश्य reloadData फोन:

- (void)searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller { 
    self.isSearching = YES; 
    [yourTableView reloadData]; 
    [controller.searchBar setShowsCancelButton:YES animated:YES]; 
} 

और की तरह अपने numberOfSectionsInTableView विधि को संशोधित:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    if(isSearching == YES) 
    { 
     yourDataArray = //filtered result; 
    } 
    else 
    { 
     yourDataAray = //unfiltered result; 
    } 
    return 1; 
} 

आशा है कि यह आपकी मदद करेगा।

+0

हाय मिडहुन, आपकी टिप्पणी के लिए धन्यवाद लेकिन यह इस तरह से काफी काम नहीं करता है। जब मैं खोज को सक्रिय करता हूं, तो SearchDisplayController तालिका दृश्य को कम कर रहा है (हालांकि हम अभी भी सामग्री देखते हैं लेकिन इसे नियंत्रित नहीं कर सकते हैं)। जब खोजबार में टेक्स्ट दर्ज किया जाता है, तो SearchDisplayTableView को फ़िल्टर किया जाता है (फ़िल्टर किया गया है या नहीं, यह मैं नियंत्रित कर सकता हूं) सामग्री, मैं दिखाना चाहता हूं। – Ron

+0

@ रोनी: जब आप टेबल व्यू टाइप करना शुरू करते हैं, तो एक ही अक्षर टाइप करने के बाद, तालिका दृश्य पिछली स्थिति (मंद नहीं) पर आता है। मुझे लगता है कि यह तुम्हारी समस्या है, क्या मैं सही हूँ? –

+0

@ रोनी: क्या आपको अभी भी समस्या है? –

0
- (void)searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller 
{ 
    // Here is a table we want to show in UISearchDisplayController 
    XXRecentSearchDataSource *recentSearch = [XXRecentSearchDataSource recentSearchDataSource]; 
    recentSearch.delegate = self; 

    // Setup default table view 
    CGRect frame = CGRectMake(0.0f, 
         CGRectGetMaxY(controller.searchBar.frame), 
         CGRectGetWidth(self.view.bounds), 
         CGRectGetHeight(self.view.bounds) - CGRectGetHeight(controller.searchBar.bounds)); 

    // Setup temporary table and remember it for future use 
    [_tempRecentSearchTableView release]; 
    _tempRecentSearchTableView = [[UITableView alloc] initWithFrame:frame 
                   style:UITableViewStylePlain]; 
    _tempRecentSearchTableView.dataSource = recentSearch; 
    _tempRecentSearchTableView.delegate = recentSearch; 

    [self performSelector:@selector(removeOverlay) withObject:nil afterDelay:.0f]; 
} 

- (void)removeOverlay 
{ 
    [[self.view.subviews lastObject] removeFromSuperview]; 
    [self.view addSubview:_tempRecentSearchTableView]; 
} 

आपको कुछ मामलों में _tempRecentSearchTableView को हटाने के लिए याद रखना चाहिए। उदाहरण के लिए:

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText 
{ 
    // Remove temporary table view 
    [_tempRecentSearchTableView removeFromSuperview]; 
} 
0

आपको पता है कि सर्चबार पर ध्यान केंद्रित किया गया है या नहीं।

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    if ([_searchBar isFirstResponder]) { 
       return [_filteredData count]; 
    } else { 

     id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:section]; 
     return [sectionInfo numberOfObjects]; 

    } 
} 

जब सेल को विन्यस्त एक ही सवाल

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

    static NSString *CellIdentifier = @"CustomCell"; 
    CustomTableViewCell *cell = (CustomTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    // Configure the cell... 
    if (cell == nil) { 
     cell = [[CustomTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 

    Entidad *e = nil; 

    if ([_searchBar isFirstResponder]) 
    { 
     e = [self.filteredData objectAtIndex:indexPath.row]; 

    } 
    else 
    { 
     e = [self.fetchedResultsController objectAtIndexPath:indexPath]; 
    } 

    cell.nameLabel.text = e.titulo; 
    cell.thumbnailImageView.image = [UIImage imageNamed:@"password-50.png"]; 
    cell.dateLabel.text = e.sitio; 

    return cell; 
} 

सेट फ़िल्टर पैरामीटर

-(void)filter:(NSString*)text 
{ 

    _filteredData = [[NSMutableArray alloc] init]; 


    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; 


    NSEntityDescription *entity = [NSEntityDescription 
            entityForName:@"Entidad" inManagedObjectContext:self.managedObjectContext]; 
    [fetchRequest setEntity:entity]; 


    NSSortDescriptor* sortDescriptor = [[NSSortDescriptor alloc] 
             initWithKey:@"titulo" ascending:YES]; 
    NSArray* sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil]; 
    [fetchRequest setSortDescriptors:sortDescriptors]; 


    if(text.length > 0) 
    { 

     NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(titulo CONTAINS[c] %@)", text]; 
     [fetchRequest setPredicate:predicate]; 
    } 

    NSError *error; 


    NSArray* loadedEntities = [self.managedObjectContext executeFetchRequest:fetchRequest error:&error]; 
    _filteredData = [[NSMutableArray alloc] initWithArray:loadedEntities]; 

    NSLog(@"%@", _filteredData); 

    [self.tableView reloadData]; 
} 

-(void)searchBar:(UISearchBar*)searchBar textDidChange:(NSString*)text 
{ 
    [self filter:text]; 
} 

-(void)searchBarCancelButtonClicked:(UISearchBar *)searchBar{ 

    [self filter:@""]; 

    [_searchBar resignFirstResponder]; 
    [_searchBar setText:@""]; 
    [_tableView reloadData]; 
} 

- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar 
{ 

    [_searchBar setShowsCancelButton:YES animated:YES]; 
} 

-(void)searchBarTextDidEndEditing:(UISearchBar *)searchBar 
{ 
    [_searchBar setShowsCancelButton:NO animated:YES]; 
} 

मैं आपकी सेवा करने की उम्मीद करते हैं, मेरी अंग्रेजी बहुत सीमित है

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