iOS 汉子转拼音 按首字母排序


    _indexArray =@[@"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", @"#"];

    

    _resultArray =@[@{@"nickname":@""},@{@"nickname":@""},@{@"nickname":@""},@{@"nickname":@""},@{@"nickname":@""},@{@"nickname":@""},@{@"nickname":@""},@{@"nickname":@"234"},@{@"nickname":@""},@{@"nickname":@""},@{@"nickname":@""}];

    

    if ([self.tableView respondsToSelector:@selector(sectionIndexBackgroundColor)]) {

        self.tableView.sectionIndexBackgroundColor = [UIColor clearColor];

    }

    [selfrelaodResultArray:_resultArray];


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

   if (tableView == self.searchDispalyController.searchResultsTableView) {

       return 1;

    }else{

        return_dataSourceArray.count;

    }

    

}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

   if (tableView == self.searchDispalyController.searchResultsTableView) {

        return_resultModelArr.count;

    }else{

       return [_dataSourceArray[section][@"value"]count];

    }


    

}

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView

{

   if (tableView == self.searchDispalyController.searchResultsTableView) {

       return nil;

    }else{

         return_indexTitleArray;

    }

   

}




-(void)relaodResultArray:(NSArray *)resultArray{

    dispatch_async(dispatch_get_global_queue(0,0), ^{

        NSMutableDictionary *dataDic = [NSMutableDictionarydictionary];

       for (NSDictionary *dicin resultArray)

        {

            

           NSMutableDictionary *tempUser = [dic mutableCopy];

            

           //汉字转拼音

           NSMutableString *string = [[NSMutableStringalloc] initWithString:tempUser[@"nickname"]];

            if (CFStringTransform((__bridgeCFMutableStringRef)string, NULL,kCFStringTransformMandarinLatin, NO)) {

                if (CFStringTransform((__bridgeCFMutableStringRef)string, NULL,kCFStringTransformStripDiacritics, NO)) {

                    tempUser[@"PinYin"] = (NSString *)string;

                }

            }

            

           NSString *firstLetter = @"#";

           if ([tempUser[@"PinYin"]length]) {

                firstLetter = [[tempUser[@"PinYin"]substringToIndex:1]uppercaseString];

            }

           if (![_indexArraycontainsObject:firstLetter]) {

                firstLetter =@"#";

            }

            

           if (!dataDic[firstLetter]){

                dataDic[firstLetter] = [NSMutableArrayarray];

            }

            [dataDic[firstLetter]addObject:tempUser];

        }

        

       NSMutableDictionary *tmpDic = [dataDic mutableCopy];

        [dataDicenumerateKeysAndObjectsUsingBlock:^(id key,id obj, BOOL *stop) {

            obj = [[objsortedArrayUsingComparator:^NSComparisonResult(id obj1,id obj2)

                    {

                       NSComparisonResult result = [[obj1[@"PinYin"]lowercaseString] compare:[obj2[@"PinYin"]lowercaseString]];

                       return result == NSOrderedDescending;// 升序

                    }]mutableCopy];

            [tmpDicsetObject:obj forKey:key];

        }];

        

        [_dataSourceArrayremoveAllObjects];

        [_indexTitleArrayremoveAllObjects];

       for (int i =0; i < _indexArray.count; ++i) {

           NSArray *tmpArr = tmpDic[_indexArray[i]];

           if (tmpArr) {

                [_indexTitleArrayaddObject:_indexArray[i]];

                [_dataSourceArrayaddObject:@{@"key":_indexArray[i],@"value":tmpArr}];

            }

        }

        [selfperformSelectorOnMainThread:@selector(reloadTableViewData)withObject:nilwaitUntilDone:true];

    });

}

猜你喜欢

转载自blog.csdn.net/mingios/article/details/42024845
今日推荐