iOS tabview添加字母索引 iOS tabview怎样添加字母索引

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

iOS tabview添加字母索引 iOS tabview怎样添加字母索引

书弋江山   2021-03-24 我要评论
想了解iOS tabview怎样添加字母索引的相关内容吗,书弋江山在本文为您仔细讲解iOS tabview添加字母索引的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点: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

猜您喜欢

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们