2010-10-13 3 views
1

salut regroupés j'ai un tableau d'objets qui doivent être triés (alphabet sur le nom)dans plusieurs tableaux pour uitableview

ArtistVO *artist1 = [ArtistVO alloc]; 
artist1.name = @"Trentemoeller"; 
artist1.imgPath = @"imgPath"; 

ArtistVO *artist2 = [ArtistVO alloc]; 
artist2.name = @"ATrentemoeller"; 
artist2.imgPath = @"imgPath2"; 


ArtistVO *artist3 = [ArtistVO alloc]; 
artist3.name = @"APhextwin"; 
artist3.imgPath = @"imgPath2";  

//NSLog(@"%@", artist1.name); 
NSMutableArray *arr = [NSMutableArray array]; 
[arr addObject:artist1]; 
[arr addObject:artist2]; 
[arr addObject:artist3]; 


NSSortDescriptor *lastDescriptor = 
[[[NSSortDescriptor alloc] 
    initWithKey:@"name" 
    ascending:YES 
    selector:@selector(localizedCaseInsensitiveCompare:)] autorelease];  

NSArray * descriptors = 
[NSArray arrayWithObjects:lastDescriptor, nil]; 
NSArray * sortedArray = 
[arr sortedArrayUsingDescriptors:descriptors];  

NSLog(@"\nSorted ..."); 
NSEnumerator *enumerator; 
enumerator = [sortedArray objectEnumerator]; 

ArtistVO *tmpARt; 
while ((tmpARt = [enumerator nextObject])) NSLog(@"%@", tmpARt.name);  

fonctionne très bien. mais maintenant je dois partager ce awway pour l'utilisation dans un uitableview groupé

self.sortedKeys =[[NSArray alloc] 
        initWithObjects:@"{search}",@"A",@"B",@"C",@"D",@"E",@"F",@"G",@"H",@"I",@"J",@"K",@"L",@"M",@"N",@"O",@"P",@"Q",@"R",@"S",@"T",@"U",@"V",@"W",@"X",@"Y",@"Z",nil]; 


NSMutableArray *arrTemp0 = [[NSMutableArray alloc] 
        initWithObjects:@"000",nil];  
NSMutableArray *arrTemp1 = [[NSMutableArray alloc] 
        initWithObjects:@"Andrew",@"Aubrey",@"Aalice", @"Andrew",@"Aubrey",@"Alice",@"Andrew",@"Aubrey",@"Alice",nil]; 
NSMutableArray *arrTemp2 = [[NSMutableArray alloc] 
        initWithObjects:@"Bob",@"Bill",@"Bianca",@"Bob",@"Bill",@"Bianca",nil]; 
NSMutableArray *arrTemp3 = [[NSMutableArray alloc] 
        initWithObjects:@"Candice",@"Clint",@"Chris",@"Candice",@"Clint",@"Chris",nil]; 
NSMutableArray *arrTemp4 = [[NSMutableArray alloc] 
        initWithObjects:@"Dandice",@"Dlint",@"Dhris",nil]; 
NSDictionary *temp =[[NSDictionary alloc] 
        initWithObjectsAndKeys:arrTemp0, @"{search}", arrTemp1,@"A",arrTemp2, 
        @"B",arrTemp3,@"C",arrTemp4,@"D",nil]; 


self.tableContents =temp; 

si tous les artistes avec la première lettre « a » venir dans une rangée ... avec « b » dans un tableau et ainsi de suite.

Dois-je faire un comparatif de chaînes ou y a-t-il une meilleure approche?

Répondre

1

Que diriez-vous:

Créer un NSMutableDictionary vide.

Parcourez toutes vos chaînes. Pour chaque chaîne:

  • Si la chaîne est vide, ignorez cette chaîne.
  • Obtient un NSString contenant le premier caractère de la chaîne convertie en majuscule. Ce sera votre clé de dictionnaire.
  • Regardez dans le dictionnaire pour voir s'il contient déjà cette clé.
  • Sinon, créez un NSMutableArray contenant uniquement votre chaîne et ajoutez-le en tant que valeur à cette nouvelle clé.
  • Si tel est le cas, ajoutez cette chaîne à la fin du tableau existant.

Fin de boucle.

+0

merci. pensé qu'il pourrait y avoir une plus petite manière comme: "retour tableau de tableau par selctor" quelque chose de similaire aux descripteurs de tri –

Questions connexes