2014-03-05 33 views
0

我有一個UITableView自定義cellView填充數組(參見函數)沒有部分。我的目標是創建一個搜索欄(這是好的)與列表索引(這是問題)。 閱讀了大量的單證後,我打算用這三種方法:「numberOfSectionsInTableView」,「sectionIndexTitlesForTableView」和「sectionForSectionIndexTitle如何從自定義單元格陣列創建NSDictionnary

如何創建functs陣列具有以下結構NSDictionnary

//in viewDidLoad 
// Initialize the functs array 
Funct *funct1 = [Funct new]; 
funct1.name = @"AAA"; 
funct1.detail = @"detail1..."; 
funct1.image = @"a.jpg"; 

Funct *funct2 = [Funct new]; 
funct2.name = @"BBB"; 
funct2.prepTime = @"detail2..."; 
funct2.image = @"b.jpg"; 

functs = [NSArray arrayWithObjects:funct, funct2, nil]; //etc. 

//For Index list: one section per letter 
NSString *letters = @"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"; 
self.indexTitlesArray = [letters componentsSeparatedByString:@" "]; 

在此先感謝

回答

0

Here你有例子項目一個很好的教程。這是你正在尋找的。

It's一個例子的TableView索引,但它有段和行

有一個在這個例子,你

static NSString *letters = @"abcdefghijklmnopqrstuvwxyz"; 

// This method returns an array of dictionaries where each key is a letter 
// and each value is a group of words corresponding to the letter. 
+ (NSArray *) wordsFromLetters { 
    NSMutableArray *content = [NSMutableArray new]; 

    for (int i = 0; i < [letters length]; i++) { 
     NSMutableDictionary *row = [[[NSMutableDictionary alloc] init] autorelease]; 
     char currentWord[WORD_LENGTH + 1]; 
     NSMutableArray *words = [[[NSMutableArray alloc] init] autorelease]; 

     for (int j = 0; j < WORD_LENGTH; j++) { 
      if (j == 0) { 
       currentWord[j] = toupper([letters characterAtIndex:i]); 
      } 
      else { 
       currentWord[j] = [letters characterAtIndex:i]; 
      } 
      currentWord[j+1] = '\0'; 
      [words addObject:[NSString stringWithCString:currentWord encoding:NSASCIIStringEncoding]]; 
     } 
     char currentLetter[2] = { toupper([letters characterAtIndex:i]), '\0'};   
     [row setValue:[NSString stringWithCString:currentLetter encoding:NSASCIIStringEncoding] 
      forKey:@"headerTitle"]; 
     [row setValue:words forKey:@"rowValues"];   
     [content addObject:row]; 
    } 

    return content; 
} 

您可以採取的主要方法,這是非常有用的一個很好的例子來自這個資源的想法,但如果你願意,我改編了代碼。我覺得良好的

NSMutableArray *funcsArray = [NSMutableArray array];//Here you add all your functs objects 
    [funcsArray addObjects: funct1, funct2...]; 
    NSSortDescriptor *sortDescriptor; 
    sortDescriptor = [[[NSSortDescriptor alloc] initWithKey:@"name" 
                ascending:YES selector:@selector(localizedCaseInsensitiveCompare:)] autorelease]; 
    NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor]; 
    NSArray *sortedArray; 
    sortedArray = [funcsArray sortedArrayUsingDescriptors:sortDescriptors]; 
    NSMutableArray *funcsArrayOrdered = [NSMutableArray array];//Here you add all your functs objects 
    NSMutableArray *funcsIndexed = [NSMutableArray array]; 
    NSMutableArray *letters = [NSMutableArray array]; 
    NSString *currentLetter = nil; 

    int numItems = [funcsArrayOrdered count]; 

    for (Funct *functObj in funcsArrayOrdered) { 
     NSLog(@"funct: %@", functObj.name); 
     numItems--; 
     NSString *string = userT.name; 
     if (string.length > 0) { 
      NSString *letter = [[string substringToIndex:1] uppercaseString]; 
      if ([currentLetter length] == 0) { 
       currentLetter = letter; 
      } 
      if (![letter isEqualToString:currentLetter] || numItems == 0) { 
       if ([letter isEqualToString:currentLetter] && numItems == 0) { 
        [letters addObject:functObj]; 
        NSMutableDictionary *dic = [NSMutableDictionary dictionary]; 
        [dic setValue:currentLetter forKey:@"headerTitle"]; 
        [dic setValue:letters forKey:@"rowValues"]; 
        [funcsIndexed addObject:dic]; 
        letters = [NSMutableArray array]; 
       }else{ 
        NSMutableDictionary *dic = [NSMutableDictionary dictionary]; 
        [dic setValue:currentLetter forKey:@"headerTitle"]; 
        [dic setValue:letters forKey:@"rowValues"]; 
        [funcsIndexed addObject:dic]; 
        letters = [NSMutableArray array]; 
        [letters addObject:functObj]; 
        currentLetter = letter; 
        if (numItems == 0 && [funcsArrayOrdered count] > 1) { 
         NSMutableDictionary *dic = [NSMutableDictionary dictionary]; 
         [dic setValue:currentLetter forKey:@"headerTitle"]; 
         [dic setValue:letters forKey:@"rowValues"]; 
         [funcsIndexed addObject:dic]; 
        } 
       } 
      }else { 
       [letters addObject:functObj]; 
      } 
     } 
    } 

現在的作品,你將有你在funcsIndexed

有序字典的陣列
+0

請添加一些示例代碼或演練,而不是僅僅只是一個鏈接的。 – Bot

+0

謝謝,但我知道這個例子,它讓我感到困惑。這是一種生成單詞的方法;它與您在我的代碼中可以看到的自定義單元格的數組結構不同。我想從這個數組中生成一個NSDictionnary .. – objM

+0

@ SonGoku86 thanks ++,dictionnary works。我在你的方法的每一步都放了很多NLog,所以現在我可以理解所有的過程。 「自上而下」的學習方法有時更像「自下而上」。 – objM

相關問題