2011-05-30 6 views
0

Je crée une barre de recherche dans un tableview mais j'ai des problèmes avec ce délégué méthode parce que je remplirai la vue du tableau avec tableau dans un autre tableau ... Je montre mon code:IOS: searchbar dans tableview

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText 
{ 
ProgramAppDelegate *appDelegate = (ProgramAppDelegate *)[[UIApplication sharedApplication] delegate]; 
[tableData removeAllObjects];// remove all data that belongs to previous search 
if([searchText isEqualToString:@""] || searchText==nil){ 
    [myTableView reloadData]; 
    return; 
} 
NSInteger counter = 0; 
for(NSString *name in appDelegate.globalArray) 
{ 
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]init]; 
    NSRange r = [name rangeOfString:searchText]; 
    if(r.location != NSNotFound) 
    { 
     if(r.location== 0)//that is we are checking only the start of the names. 
     { 
      [tableData addObject:name]; 
     } 
    } 
    counter++; 
    [pool release]; 
} 
[myTableView reloadData]; 
} 

Vous pouvez voir le code "pour (NSString * nom dans appDelegate.globalArray)" cela ne fonctionne pas parce que je remplis la table avec des éléments de tableaux dans ce tableau global, je fais un exemple

Dans une rangée de mon vue de la table il y a une uitableviewcell et à l'intérieur il y a quatre étiquette; j'écris ces étiquettes avec de la ficelle de cette globalArray mais de cette façon:

[cell.label1 setText:[[appDelegate.globalArray objectAtIndex:indexPath.row]objectAtIndex:1]]; 
[cell.label2 setText:[[appDelegate.globalArray objectAtIndex:indexPath.row]objectAtIndex:2]]; 
[cell.label3 setText:[[appDelegate.globalArray objectAtIndex:indexPath.row]objectAtIndex:3]]; 
[cell.label4 setText:[[appDelegate.globalArray objectAtIndex:indexPath.row]objectAtIndex:4]]; 

puis dans la méthode de délégué pour searchbar le code « (nom NSString * en appDelegate.globalArray) » ne fonctionnent pas, comment puis-je changer mon code?

* * JE NE VEUX QUE JE VÉRIFIER SEULEMENT LABEL1 pour la recherche

Répondre

0

Le problème ici est que globalArray est un tableau de tableaux. Donc, la boucle devrait être quelque chose comme.

for(NSArray *rowArray in appDelegate.globalArray) 
{ 
    for (NSString *name in rowArray) { 
     // Do your processing here.. 
    } 
} 

En utilisant tableData

Dans votre tableView:cellForRowAtIndexPath:, faites ce

[cell.label1 setText:[[tableData objectAtIndex:indexPath.row]objectAtIndex:1]]; 
[cell.label2 setText:[[tableData objectAtIndex:indexPath.row]objectAtIndex:2]]; 
[cell.label3 setText:[[tableData objectAtIndex:indexPath.row]objectAtIndex:3]]; 
[cell.label4 setText:[[tableData objectAtIndex:indexPath.row]objectAtIndex:4]]; 

Dans votre numberOfSectionsInTableView:, return 1;

Dans votre tableView:numberOfRowsInSection:, return [tableData count];

Vous devez inclure une méthode qui permet de réinitialiser les données de recherche au contenu d'origine lorsque l'utilisateur arrête la recherche.

- (void)resetSearchData { 
    // Get appDelegate first. 

    self.tableData = [NSArray arrayWithArray:appDelegate.globalArray]; 
} 
+0

maintenant aucun problème, mais ne cherche pas, quand j'écris un nom à l'intérieur searchbar, éléments restent les mêmes à Tableview – CrazyDev

+0

Deux choses ici - '[TableData addObject: nom];' doit être '[TableData addObject: rowArray]; 'et vous devriez remplacer' appDelegate.globalArray' par 'tableData' dans la méthode' tableView: cellForRowAtIndexPath: '. –

+0

première chose ok; deuxième chose que je fais cela dans loadview: [tableData addObjectsFromArray: appDelegate.globalArray]; – CrazyDev