本文实例为大家分享了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
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持亿速云。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。