我從後臺獲取到數據,想做的效果就是把獲取到的數據文字的首字母提取出來做成索引條顯示,便於用戶選擇,用到的知識就是,將漢字轉換成拼音再提取首字母轉換大小寫,再排序,然後顯示在索引條上
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];
});
}