我从后台获取到数据,想做的效果就是把获取到的数据文字的首字母提取出来做成索引条显示,便于用户选择,用到的知识就是,将汉字转换成拼音再提取首字母转换大小写,再排序,然后显示在索引条上
demo:https://github.com/tuwanli/ChineseToChar
也许可爱的后台人员给你的数据就是类似这种,意思就是数组里面一堆字典,我只需要把其中某个键值对取出来,然后把文字变成拼音,取出来首字母给它排下顺序,换成大写字母就OK了,就是这么简单
直接上关键代码
这个方法就是将你获取的数据汉字抽取出来,获取首字母,放进字典,键是首字母,值是汉字
- (NSDictionary *)createCharacter:(NSMutableArray *)strArr
{
NSMutableDictionary *dict = [NSMutableDictionarydictionary];
for (NSDictionary *stringdictin strArr) {
NSString *string = stringdict[@"name"];
if ([stringlength]) {
NSMutableString *mutableStr = [[NSMutableStringalloc]initWithString:string];
if (CFStringTransform((__bridgeCFMutableStringRef)mutableStr,0,kCFStringTransformMandarinLatin,NO)) {
}
if (CFStringTransform((__bridgeCFMutableStringRef)mutableStr,0,kCFStringTransformStripDiacritics,NO)) {
NSString *str = [NSStringstringWithString:mutableStr];
str = [struppercaseString];
NSMutableArray *subArray = [dictobjectForKey:[str substringToIndex:1]];
if (!subArray) {
subArray = [NSMutableArrayarray];
[dictsetObject:subArrayforKey:[str substringToIndex:1]];
}
[subArrayaddObject:string];
}
}
}
return dict;
}
这个方法是将字母按照A~Z的顺序进行排列显示
_dataArray = [self.allKeysDict.allKeyssortedArrayUsingComparator:^NSComparisonResult(id obj1,id obj2) {
NSString *letter1 = obj1;
NSString *letter2 = obj2;
if (KCNSSTRING_ISEMPTY(letter2)) {
returnNSOrderedDescending;
}elseif ([letter1characterAtIndex:0] < [letter2characterAtIndex:0]) {
returnNSOrderedAscending;
}
returnNSOrderedDescending;
}];
再接下来就是写表了
//创建右侧索引表,返回需要显示的索引表数组
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return self.dataArray.count;
}
- (NSArray *) sectionIndexTitlesForTableView:(UITableView *)tableView {
return self.dataArray;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [(NSArray*)self.allKeysDict[self.dataArray[section]]count];
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
returnself.dataArray[section];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
staticNSString *cellID =@"cell";
UITableViewCell *cell = [tableViewdequeueReusableCellWithIdentifier:cellID];
if (cell ==nil) {
cell = [[UITableViewCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:cellID];
}
cell.textLabel.text = [self.allKeysDict[self.dataArray[indexPath.section]]objectAtIndex:indexPath.row];
return cell;
}
新增加搜索功能
seachVC = [[UISearchControlleralloc]initWithSearchResultsController:nil];
seachVC.searchResultsUpdater =self;
seachVC.dimsBackgroundDuringPresentation =false;
[seachVC.searchBarsizeToFit];
_tableView.tableHeaderView =seachVC.searchBar;
声明实现协议UISearchResultsUpdating
#pragma mark - searchController delegate
- (void)updateSearchResultsForSearchController:(UISearchController *)searchController {
// [self.littleArray removeAllObjects];
NSPredicate *searchPredicate = [NSPredicatepredicateWithFormat:@"SELF CONTAINS[c] %@",searchController.searchBar.text];
self.littleArray = [[self.dataArrayfilteredArrayUsingPredicate:searchPredicate] mutableCopy];
NSLog(@"--------------------%@",_littleArray);
dispatch_async(dispatch_get_main_queue(), ^{
[self.tableViewreloadData];
});
}