当前位置: 移动技术网 > 移动技术>移动开发>IOS > iOS tabview如何添加字母索引

iOS tabview如何添加字母索引

2019年07月24日  | 移动技术网移动技术  | 我要评论

本文实例为大家分享了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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网