2010-01-08 4 views
0

J'ai un TabBar, NavBar, SearchBar avec ScopeBar sur mon écran. Je peux rechercher des données via un serveur distant et répertorier le contenu. J'ai donc NSMutableArray listContent et un filteredListContent comme dans l'exemple d'Apple (TableSearch - http://developer.apple.com/iphone/library/samplecode/TableSearch/index.html):iPhone - UITableView - données perdues si je reviens

Maintenant, j'ajouté dans

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 

ces lignes:

testDetailViewController *testDetailViewController = [[TestDetailViewController alloc] initWithNibName:@"TestDetailView" bundle:[NSBundle mainBundle]]; 
    testDetailViewController.title = testClass.name; 
    testDetailViewController.myKey = testClass.keyId; 
    [[self navigationController] pushViewController:testDetailViewController animated:YES]; 
    [testDetailViewController release]; 
    testDetailViewController = nil; 

En raison de la NavigationBar , il y a un bouton "retour". Si je clique sur ce bouton, le TableView est vide, aucun match/hits. Qu'est-ce que je dois faire, donc le contenu sera toujours là?

Quelqu'un sait-il?

Merci beaucoup d'avance & Cordialement.

Code Source:

@implementation SearchViewController 

@synthesize listContent, filteredListContent, savedSearchTerm, savedScopeButtonIndex, searchWasActive; 

- (void)viewDidLoad { 
    // restore search settings if they were saved in didReceiveMemoryWarning. 
    if (self.savedSearchTerm) { 
     [self.searchDisplayController setActive:self.searchWasActive]; 
     [self.searchDisplayController.searchBar setSelectedScopeButtonIndex:self.savedScopeButtonIndex]; 
     [self.searchDisplayController.searchBar setText:savedSearchTerm]; 
     self.savedSearchTerm = nil; 
    } 
} 

- (void)viewDidUnload { 
    // Save the state of the search UI so that it can be restored if the view is re-created. 
    self.searchWasActive = [self.searchDisplayController isActive]; 
    self.savedSearchTerm = [self.searchDisplayController.searchBar text]; 
    self.savedScopeButtonIndex = [self.searchDisplayController.searchBar selectedScopeButtonIndex]; 
    self.filteredListContent = nil; 
} 

- (void)dealloc { 
    [listContent release]; 
    [filteredListContent release]; 
    [super dealloc]; 
} 

- (void)setData { 
    self.listContent = [NSMutableArray arrayWithCapacity:3]; 
    [self.listContent addObject:[SearchObjects itemWithType:@"AAA" name:@"Test1"]]; 
    [self.listContent addObject:[SearchObjects itemWithType:@"BBB" name:@"Test2"]]; 
    [self.listContent addObject:[SearchObjects itemWithType:@"BBB" name:@"Test3"]]; 

    // create a filtered list 
    self.filteredListContent = [NSMutableArray arrayWithCapacity:[self.listContent count]]; 
    [self.tableView reloadData]; 
    self.tableView.scrollEnabled = YES; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    //If the requesting table view is the search display controller's table view, return the count of the filtered list, otherwise return the count of the main list. 
    if (tableView == self.searchDisplayController.searchResultsTableView) { 
     return [self.filteredListContent count]; 
    } else { 
     return [self.listContent count]; 
    } 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    static NSString *kCellID = @"cellID"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellID]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kCellID] autorelease]; 
     cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 
    } 

    /* If the requesting table view is the search display controller's table view, configure the cell using the filtered content, otherwise use the main list. */ 
    SearchObjects *searchObject = nil; 
    if (tableView == self.searchDisplayController.searchResultsTableView) { 
     searchObject = [self.filteredListContent objectAtIndex:indexPath.row]; 
    } else { 
     searchObject = [self.listContent objectAtIndex:indexPath.row]; 
    } 
    cell.textLabel.text = searchObject.name; 
    return cell; 
} 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
    // HERE IS THE SOURCE CODE FOR PUSHING TO THE NEXT VIEW 
} 

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar { 
    // DO SOME CALCULATIONS… AND THE setData METHOD IS CALLED 
} 

- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope { 
    /* Update the filtered array based on the search text and scope. */ 
    [self.filteredListContent removeAllObjects]; // First clear the filtered array. 

    /* Search the main list for whose type matches the scope (if selected) and whose name matches searchText; add items that match to the filtered array. */ 
    for (SearchObjects *searchObject in listContent) { 
     if ([scope isEqualToString:@"All"] || [searchObject.type isEqualToString:scope]) { 
     NSComparisonResult result = [searchObject.name compare:searchText options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch) range:NSMakeRange(0, [searchText length])]; 
      if (result == NSOrderedSame) { 
       [self.filteredListContent addObject:searchObject]; 
      } 
     } 
    } 
} 

- (void)filterContentForScope:(NSString*)scope { 
    /* Update the filtered array based on the search text and scope. */ 
    [self.filteredListContent removeAllObjects]; // First clear the filtered array. 

    /* Search the main list for whose type matches the scope (if selected); add items that match to the filtered array. */ 
    for (SearchObjects *searchObject in listContent) { 
     if ([scope isEqualToString:@"All"] || [searchObject.type isEqualToString:scope]) { 
      [self.filteredListContent addObject:searchObject]; 
     } 
    } 
} 

- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString { 
    [self filterContentForScope:[[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:[self.searchDisplayController.searchBar selectedScopeButtonIndex]]];  
    // Return YES to cause the search result table view to be reloaded. 
    return YES; 
} 

- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchScope:(NSInteger)searchOption { 
    [self filterContentForScope:[[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:searchOption]]; 
    // Return YES to cause the search result table view to be reloaded. 
    return YES; 
} 
@end 

Répondre

0

Il est résolu. C'était un problème qui n'est pas évident avec le code source donné. Une erreur s'est produite dans ma logique.

1

Vous n'ont généralement rien à faire dans ce cas, les données doivent rester en place. Y a-t-il quelque chose qui décharge les données? Avez-vous une fonction viewWillDisappear qui décharge votre baie? Faites-vous une partie de la configuration de tableau dans viewWillAppear. Mettez une instruction log au début de vos méthodes pour savoir quand elles sont appelées, cela vous donnera une image plus claire de ce qui se passe.

+0

J'ai ajouté le code source de mon ViewController où se trouve TableView. Il n'y a pas d'appels viewWillAppear/viewWillDisappear. – Tim

Questions connexes