2010-10-13 51 views
1

您好我有需要被排序(字母表上名)對象的數組目標c溢出陣列中的多個陣列對的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);  

工作正常。但現在我需要在一個分組的UITableView

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; 

拆分此awway彌補使用情況,以便與第一個字母的所有藝術家「」進來一個陣......與「B」在一個陣列等。

我需要做一些字符串比較還是有更好的方法?

回答

1

如何:

創建一個空的NSMutableDictionary。

循環遍歷所有的字符串。對於每個字符串:

  • 如果字符串爲空,請忽略此字符串。
  • 獲取包含轉換爲大寫字符串的第一個字符的NSString。這將是你的字典鍵。
  • 查看字典中是否已經包含此密鑰。
  • 如果不是,則創建一個僅包含字符串的新NSMutableArray,並將其作爲值添加到此新密鑰中。
  • 如果是這樣,請將此字符串添加到現有數組的末尾。

循環結束。

+0

謝謝。認爲可能有一個更小的方法,如:「通過選擇器從數組中返回數組」類似於排序描述符 – 2010-10-14 12:13:53