_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.tableViewrespondsToSelector:@selector(sectionIndexBackgroundColor)]) {
self.tableView.sectionIndexBackgroundColor = [UIColorclearColor];
}
[selfrelaodResultArray:_resultArray];
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
if (tableView ==self.searchDispalyController.searchResultsTableView) {
return1;
}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) {
returnnil;
}else{
return_indexTitleArray;
}
}
-(void)relaodResultArray:(NSArray *)resultArray{
dispatch_async(dispatch_get_global_queue(0,0), ^{
NSMutableDictionary *dataDic = [NSMutableDictionarydictionary];
for (NSDictionary *dicin resultArray)
{
NSMutableDictionary *tempUser = [dicmutableCopy];
//汉字转拼音
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 = [dataDicmutableCopy];
[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:objforKey: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];
});
}