注册

iosdemo好友列表索引

demo有这个方法,要实现他么
已邀请:
是这个方法
- (NSMutableArray *)sortDataArray:(NSArray *)dataArray

{

    //建立索引的核心

    UILocalizedIndexedCollation *indexCollation = [UILocalizedIndexedCollation currentCollation];

    

    [self.sectionTitles removeAllObjects];

    [self.sectionTitles addObjectsFromArray:[indexCollation sectionTitles]];

    

    //返回27,是a-z和#

    NSInteger highSection = [self.sectionTitles count];

    //tableView 会被分成27个section

    NSMutableArray *sortedArray = [NSMutableArray arrayWithCapacity:highSection];

    for (int i = 0; i <= highSection; i++) {

        NSMutableArray *sectionArray = [NSMutableArray arrayWithCapacity:1];

        [sortedArray addObject:sectionArray];

    }

    

    //名字分section

    for (EMBuddy *buddy in dataArray) {

        //getUserName是实现中文拼音检索的核心,见NameIndex类

        NSString *firstLetter = [ChineseToPinyin pinyinFromChineseString:buddy.username];

        NSInteger section = [indexCollation sectionForObject:[firstLetter substringToIndex:1] collationStringSelector:@selector(uppercaseString)];

        

        NSMutableArray *array = [sortedArray objectAtIndex:section];

        [array addObject:buddy];

    }

    

    //每个section内的数组排序

    for (int i = 0; i < [sortedArray count]; i++) {

        NSArray *array = [[sortedArray objectAtIndex:i] sortedArrayUsingComparator:^NSComparisonResult(EMBuddy *obj1, EMBuddy *obj2) {

            NSString *firstLetter1 = [ChineseToPinyin pinyinFromChineseString:obj1.username];

            firstLetter1 = [[firstLetter1 substringToIndex:1] uppercaseString];

            

            NSString *firstLetter2 = [ChineseToPinyin pinyinFromChineseString:obj2.username];

            firstLetter2 = [[firstLetter2 substringToIndex:1] uppercaseString];

            

            return [firstLetter1 caseInsensitiveCompare:firstLetter2];

        }];

        

        

        [sortedArray replaceObjectAtIndex:i withObject:[NSMutableArray arrayWithArray:array]];

    }

    

    NSLog(@"ssssssssss===%@",sortedArray);

    return sortedArray;

}
看你的需求啊,有需要就实现啊。

要回复问题请先登录注册