本文實例為大家分享了iOS tabview添加字母索引的具體代碼,供大家參考,具體內容如下
文章轉載自大神源碼傳送門
1、將漢字轉換成首字母
//系統獲取首字母
- (NSString *) pinyinFirstLetter:(NSString*)sourceString {
NSMutableString *source = [sourceString mutableCopy];
CFStringTransform((__bridge CFMutableStringRef)source, NULL, kCFStringTransformMandarinLatin, NO);
CFStringTransform((__bridge CFMutableStringRef)source, NULL, kCFStringTransformStripDiacritics, NO);//這一行是去聲調的
return source;
}
2、和tabview綁定的方法
#import "ViewController.h"
#import "BMChineseSort.h"
#import "Person.h"
@interface ViewController (){
NSMutableArray<Person *> *dataArray;
}
//排序后的出現過的拼音首字母數組
@property(nonatomic,strong)NSMutableArray *indexArray;
//排序好的結果數組
@property(nonatomic,strong)NSMutableArray *letterResultArr;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//模擬數據加載 dataArray中得到Person的數組
[self loadData];
//BMChineseSort 文件包含兩個對單元格數據和右側字母的數組排序函數
//根據Person對象的 name 屬性 按中文 對 Person數組 排序
//每一個單元格的數據,排序好了的
self.indexArray = [BMChineseSort IndexWithArray:dataArray Key:@"name"];
//左側的字母數組,已經排序好了
self.letterResultArr = [BMChineseSort sortObjectArray:dataArray Key:@"name"];
UITableView *table = [[UITableView alloc] initWithFrame:self.view.frame];
table.delegate = self;
table.dataSource = self;
[self.view addSubview:table];
}
//加載模擬數據
-(void)loadData{
NSArray *stringsToSort=[NSArray arrayWithObjects:
@"李白",@"張三",
@"重慶",@"重量",
@"調節",@"調用",
@"小白",@"小明",@"千玨",
@"黃家駒", @"鼠標",@"hello",@"多美麗",@"肯德基",@"##",
nil];
//模擬網絡請求接收到的數組對象 Person數組
dataArray = [[NSMutableArray alloc] initWithCapacity:0];
for (int i = 0; i<[stringsToSort count]; i++) {
Person *p = [[Person alloc] init];
p.name = [stringsToSort objectAtIndex:i];
p.number = i;
[dataArray addObject:p];
}
}
#pragma mark - UITableView -
//section的titleHeader
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
return [self.indexArray objectAtIndex:section];
}
//section行數
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return [self.indexArray count];
}
//每組section個數
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [[self.letterResultArr objectAtIndex:section] count];
}
//section右側index數組
-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{
return self.indexArray;
}
//點擊右側索引表項時調用 索引與section的對應關系
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index{
return index;
}
//返回cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CELL"];
if (cell == nil){
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CELL"];
}
//獲得對應的Person對象<替換為你自己的model對象>
Person *p = [[self.letterResultArr objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
cell.textLabel.text = p.name;
return cell;
}
@end
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。