2014-01-06 5 views
0

Мне интересно, как сортировать китайский символ в «#» вместо A-Z.как отсортировать китайский символ в # в UILocalizedIndexedCollation

любые комментарии очень ценятся.

-(NSArray *)partitionObjects:(NSArray *)array collationStringSelector:(SEL)selector 
{ 
    self.collation = [UILocalizedIndexedCollation currentCollation]; 
    NSInteger sectionCount = [[self.collation sectionTitles] count];//section count is take from sectionTitles and not sectionIndexTitles 
    NSMutableArray *unsortedSections = [NSMutableArray arrayWithCapacity:sectionCount]; 
    //create an array to hold the data for each section 
    for(int i = 0; i < sectionCount; i++) 
    { 
     [unsortedSections addObject:[NSMutableArray array]]; 
    } 
    //put each object into a section 
    for (id object in array) 
    { 
     NSInteger index = [self.collation sectionForObject:object collationStringSelector:selector]; 
     [[unsortedSections objectAtIndex:index] addObject:object]; 
    } 
    NSMutableArray *sections = [NSMutableArray arrayWithCapacity:sectionCount]; 
    //sort each section 
    for (NSMutableArray *section in unsortedSections) 
    { 
     [sections addObject:[self.collation sortedArrayFromArray:section collationStringSelector:selector]]; 
    } 
    return sections; 
} 

enter image description here

ответ

0

Это то, что я в конечном итоге делает

-(NSArray *)partitionObjects:(NSArray *)array collationStringSelector:(SEL)selector 
{ 
    self.collation = [UILocalizedIndexedCollation currentCollation]; 
    NSInteger sectionCount = [[self.collation sectionTitles] count];//section count is take from sectionTitles and not sectionIndexTitles 
    NSMutableArray *unsortedSections = [NSMutableArray arrayWithCapacity:sectionCount]; 

    //create an array to hold the data for each section 
    for(int i = 0; i < sectionCount; i++) 
    { 
     [unsortedSections addObject:[NSMutableArray array]]; 
    } 

    if ([self.catString isEqualToString:ARTISTS]) 
    { 
     //put each object into a section 
     for (id object in array) 
     { 
      if (!object) 
      { 
       continue; 
      } 
      NSInteger index = [self.collation sectionForObject:object collationStringSelector:selector]; 
      [[unsortedSections objectAtIndex:index] addObject:object]; 
     } 
    } 
    else 
    { 
     NSInteger index; 
     for (id object in array) 
     { 
      Song *songItem = object; 
      NSString* charIndex; 

      if([songItem.songName length]<=2) 
      { 
       charIndex = [songItem.songName substringToIndex:1]; 
      } 
      else if([songItem.songName length]<=3) 
      { 
       charIndex = [songItem.songName substringToIndex:2]; 
      } 
      else if([songItem.songName length]<=4) 
      { 
       charIndex = [songItem.songName substringToIndex:3]; 
      } 
      else if([songItem.songName length]>=5) 
      { 
       charIndex = [songItem.songName substringToIndex:4]; 
      } 
      NSRegularExpression *regex = [[NSRegularExpression alloc] 
              initWithPattern:@"[a-zA-Z]" options:0 error:NULL]; 
      NSUInteger matches = [regex numberOfMatchesInString:charIndex options:0 
                  range:NSMakeRange(0, [charIndex length])]; 
      if (matches >=2) 
      { 
       index = [self.collation sectionForObject:object collationStringSelector:selector]; 
       [[unsortedSections objectAtIndex:index] addObject:object]; 
      } 
      else 
      { 
       index = 26; //add object to last index "#" 
       [[unsortedSections objectAtIndex:index] addObject:object]; 
      } 
      songItem = nil; 
     } 
    } 
    NSMutableArray *sections = [NSMutableArray arrayWithCapacity:sectionCount]; 

    //sort each section 
    for (NSMutableArray *section in unsortedSections) 
    { 
     [sections addObject:[self.collation sortedArrayFromArray:section collationStringSelector:selector]]; 
    } 
    return sections; 
} 
+0

Я думаю, мой вопрос, почему вы используете UILocalizedIndexedCollation вообще. – matt

+0

для индексации, но я хочу, чтобы китайский символ отсортировался на «#» вместо – Desmond

+0

«Для индексирования». Но вы делаете все индексирование самостоятельно. Ваши данные не локализованы. Это просто английский алфавит плюс правило о китайском. Вы _fighting_ UILocalizedIndexedCollation, а не _using_ it. У вас будет более легкое время, если UILocalizedIndexedCollation вообще не будет в истории. – matt

Смежные вопросы