comment append une barre de search dans uitableview?

J'ai un NSMutableArray affiché dans un UITableView , et j'ai ajouté quelques éléments là.

Par exemple, les noms d'éléments sont First, FirstTwo, Second, SecondTwo, Third, ThirdTwo.

Maintenant, je veux append une barre de search à l'écran. Dans cette barre de search lorsque je tape F, la table ne devrait montrer que First et FirstTwo.

Comment dois-je faire cela?

La meilleure façon de vous y prendre est de suivre un tutoriel ici . La partie que vous cherchez est la suivante:

 - (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSSsortingng *)searchText { [tableData removeAllObjects];// remove all data that belongs to previous search if([searchText isEqualToSsortingng:@""]searchText==nil){ [myTableView reloadData]; return; } NSInteger counter = 0; for(NSSsortingng *name in dataSource) { NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]init]; NSRange r = [name rangeOfSsortingng: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]; } 
  searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)]; searchBar.delegate = self; self.tableView.tableHeaderView = searchBar; 

Si vous utilisez la barre de search. alors vous pouvez searchr votre contenu de la vue de table en utilisant la méthode déléguée de la barre de search

 (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSSsortingng *)searchText { // myArray is main array for Table View // tempMainArray which store myArray Data myArray = [[NSMutableArray alloc]initWithArray:tempMainArray]; NSMutableArray *searchArray = [[NSMutableArray alloc]init]; [searchArray removeAllObjects];// remove all data that belongs to previous search if([searchText isEqualToSsortingng:@""]|| searchText==nil){ [myArray removeAllObjects]; myArray = tempMainArray; [_objTableView reloadData]; return; } NSInteger counter = 0; for(NSSsortingng *name in myArray) { NSRange r = [name rangeOfSsortingng:searchText]; if(r.location != NSNotFound) { if(r.location== 0)//that is we are checking only the start of the names dynamicaly. { [searchArray addObject:name]; } } counter++; } if (searchArray.count > 0) { [myArray removeAllObjects]; myArray = searchArray; [_objTableView reloadData]; } else { [myArray removeAllObjects]; myArray = tempMainArray; [_objTableView reloadData]; } } 

Je l'ai fait moi-même et ça marche très bien.

Déclarez deux arrays mutables. Une contenant toutes datatables et une seconde pour stocker datatables filtrées. Ici, searchuser est la barre de search. aray est le tableau principal qui stocke toutes datatables. Définissez la propriété de capitalisation de la barre de search sur la phrase pour que cela fonctionne correctement.

 - (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSSsortingng *)searchText { [searchuser.text lowercaseSsortingng]); filteredarray=[[NSMutableArray alloc]init]; int l=(int)[searchuser.text length]; if (l>0) { NSMutableSsortingng * data=[[NSMutableSsortingng alloc]init]; int a=(int)[aray count]; for (int i=0;i<=a-1;i++) { data=[aray objectAtIndex:i]; if ([searchuser.text isEqualToSsortingng:data]) { [filteredarray addObject:data]; } if ([data length]>l) { NSMutableSsortingng * ssortingng=[[NSMutableSsortingng alloc]initWithSsortingng:[data subssortingngWithRange:NSMakeRange(0, l)]]; if ([searchuser.text isEqualToSsortingng:ssortingng]) { [filteredarray addObject:data]; } } } } else { filteredarray=aray; } [tableview reloadData]; }