【问题标题】:how to sort Chinese character into # in UILocalizedIndexedCollation如何在 UILocalizedIndexedCollat​​ion 中将汉字排序为#
【发布时间】:2014-01-06 04:16:52
【问题描述】:

我想知道如何将汉字排序为“#”而不是 A-Z。

非常感谢任何 cmets。

-(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;
}

【问题讨论】:

    标签: ios objective-c uitableview uilocalizedcollation


    【解决方案1】:

    这就是我最终做的事情

    -(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;
    }
    

    【讨论】:

    • 我想我的问题是你为什么要使用 UILocalizedIndexedCollat​​ion。
    • 用于索引,但我希望将汉字排序为“#”
    • "为了索引" 但是你自己做所有的索引。您的数据本地化。它只是英文字母加上关于中文的规则。你是在战斗 UILocalizedIndexedCollat​​ion,而不是使用它。如果 UILocalizedIndexedCollat​​ion 根本不在故事中,你会过得更轻松。
    • 以下是如何使用 A-Z 部分构建您自己的基于部分的表视图模型数据:github.com/mattneub/Programming-iOS-Book-Examples/blob/master/… 如果您只调整 for 循环中显示的 @987654324,您将拥有更简单的时间@实现添加中文标题处理。
    • @matt UILocalizedIndexedCollat​​ion 工作正常,但我很难像 iPod 那样进行排序。对于非 A-Z 到“#”,我最初执行了您之前提到的 for 循环的方法...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-09-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多