2012-10-11 4 views
0

안녕하세요 저는 테이블보기에서 정렬 기능을 구현 한 응용 프로그램 중 하나를 만들었습니다. 정렬 방법은 iOS 4와 5에서 잘 작동하지만 응용 프로그램을 테스트하려고 할 때 아이폰 OS 6, 그것은 아이폰 OS 6 의 정렬 방법에 오류가 enter image description here배열을 iOS 6의 테이블보기로 정렬

방법을 도와주세요 보여줍니다 -

-(void)setupIndexData{ 
    self.arrayOfCharacters =[[NSMutableArray alloc]init]; 
    self.objectForCharacter=[[NSMutableDictionary alloc]init]; 

    NSNumberFormatter *formatter =[[NSNumberFormatter alloc]init]; 
    NSMutableArray *arrayOfNames =[[NSMutableArray alloc]init]; 
    NSString *numbericSection = @"#"; 
    NSString *firstLetter; 
    for (NSDictionary *item in self.mCompanyarray) { 

     firstLetter = [[[item valueForKey:@"Company"]description] substringToIndex:1]; 

     // Check if it's NOT a number 
     if ([formatter numberFromString:firstLetter] == nil) { 

      /** 
      * If the letter doesn't exist in the dictionary go ahead and add it the 
      * dictionary. 
      * 
      * ::IMPORTANT:: 
      * You HAVE to removeAllObjects from the arrayOfNames or you will have an N + 1 
      * problem. Let's say that start with the A's, well once you hit the 
      * B's then in your table you will the A's and B's for the B's section. Once 
      * you hit the C's you will all the A's, B's, and C's, etc. 
      */ 
      if (![objectForCharacter objectForKey:firstLetter]) { 

       [arrayOfNames removeAllObjects]; 

       [arrayOfCharacters addObject:firstLetter]; 
      } 

      [arrayOfNames addObject:item]; 

      /** 
      * Need to autorelease the copy to preven potential leak. Even though the 
      * arrayOfNames is released below it still has a retain count of +1 
      */ 
      [objectForCharacter setObject:[[arrayOfNames copy] autorelease] forKey:firstLetter]; 

     } else { 

      if (![objectForCharacter objectForKey:numbericSection]) { 

       [arrayOfNames removeAllObjects]; 

       [arrayOfCharacters addObject:numbericSection]; 
      } 

      [arrayOfNames addObject:item]; 

      [objectForCharacter setObject:[[arrayOfNames copy] autorelease] forKey:numbericSection]; 
     } 
    } 

    [formatter release]; 
    [arrayOfNames release]; 

    [self.mCompaniesTableView reloadData]; 
} 

감사

+2

무엇 오류 메시지가 무엇입니까? – iDev

+0

iOS 6에서 오류를 표시하지 않습니다. iOS 5 및 iOS 6에서 정렬을 구현하는 데 동일한 코드를 사용합니다. –

+0

이 코드는 지나치게 복잡해 보입니다. 무엇을 성취하려고합니까? – Eiko

답변

2

나는 당신의 데이터를 정렬하고 인덱스 할 UILocalizedIndexedCollation을 사용하십시오. 그렇게하면 앱에서 여러 언어 등을 지원할 수 있습니다.

참고 : 아래 코드는 테스트하지 않았지만 이론은 있습니다.

첫째, 인덱스 데이터를 저장하는 @property를 만들 :이 같은

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView 
{ 
    return [[UILocalizedIndexedCollation currentCollation] sectionIndexTitles]; 
} 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    //we use sectionTitles and not sections 
    return [[[UILocalizedIndexedCollation currentCollation] sectionTitles] count]; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return [[self.indexedSections objectForKey:[NSNumber numberWithInteger:section]] count]; 
} 

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section 
{ 
    BOOL showSection = [[self.indexedSections objectForKey:[NSNumber numberWithInteger:section] count] != 0; 

    //only show the section title if there are rows in the section 
    return (showSection) ? [[[UILocalizedIndexedCollation currentCollation] sectionTitles] objectAtIndex:section] : nil; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    id object = [[self.indexedSections objectForKey:[NSNumber numberWithInteger:indexPath.section]] objectAtIndex:indexPath.row]; 

    // configure the cell 
} 

- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index 
{ 
    //sectionForSectionIndexTitleAtIndex: is a bit buggy, but is still useable 
    return [[UILocalizedIndexedCollation currentCollation] sectionForSectionIndexTitleAtIndex:index]; 
} 

그리고 마지막으로 인덱스 :

이 같은 테이블을 설정

@property (nonatomic, strong) NSDictionary *indexedSections; 

- (void) setupIndexData 
{ 
    // asynchronously sort 
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0),^
    { 
     // create a dictionary to store an array of objects for each section 
     NSMutableDictionary *tempSections = [NSMutableDictionary dictionary]; 

     // iterate through each dictionaey in the list, and put them into the correct section 
     for (NSDictionary *item in self.mCompanyarray) 
     { 
      // get the index of the section (Assuming the table index is showing A-#) 
      NSInteger indexName = [[UILocalizedIndexedCollation currentCollation] sectionForObject:[item valueForKey:@"Company"] collationStringSelector:@selector(description)]; 

      NSNumber *keyName = [NSNumber numberWithInteger:indexName]; 

      // if an array doesnt exist for the key, create one 
      NSMutableArray *arrayName = [tempSections objectForKey:keyName]; 
      if (arrayName == nil) 
      { 
       arrayName = [NSMutableArray array]; 
      } 

      // add the dictionary to the array (add the actual value as we need this object to sort the array later) 
      [arrayName addObject:[item valueForKey:@"Company"]]; 

      // put the array with new object in, back into the dictionary for the correct key 
      [tempSections setObject:arrayName forKey:keyName]; 
     } 

     /* now to do the sorting of each index */ 

     NSMutableDictionary *sortedSections = [NSMutableDictionary dictionary]; 

     // sort each index array (A..Z) 
     [tempSections enumerateKeysAndObjectsUsingBlock:^(id key, id array, BOOL *stop) 
     { 
      // sort the array - again, we need to tell it which selctor to sort each object by 
      NSArray *sortedArray = [[UILocalizedIndexedCollation currentCollation] sortedArrayFromArray:array collationStringSelector:@selector(description)]; 
      [sortedSections setObject:[NSMutableArray arrayWithArray:sortedArray] forKey:key]; 
     }]; 

     // set the global sectioned dictionary 
     self.indexedSections = sortedSections; 

     dispatch_async(dispatch_get_main_queue() ,^{ 
      // reload the table view (on the main thread) 
      [self.mCompaniesTableView reloadData]; 
     }); 
    }); 
} 
관련 문제