简单实现UITableView索引功能(中英文首字母索引)(一) ByH罗

时间:2021-07-17 16:06:52

UITableView索引功能是常见的,主要是获取中英文的首字母并排序,系统自带获取首字母

//系统获取首字母
- (NSString *) pinyinFirstLetter:(NSString*)sourceString {
NSMutableString
*source = [sourceString mutableCopy];
CFStringTransform((__bridge CFMutableStringRef)source, NULL, kCFStringTransformMandarinLatin, NO);
CFStringTransform((__bridge CFMutableStringRef)source, NULL, kCFStringTransformStripDiacritics, NO);//这一行是去声调的
return source;
}

 

PinYin.h文件是网上比较常用的获取中英文首字母方法,NSString+PinYin.h是别人写的获取首字母并对首字母进行字典分类的NSString Categrory,这样大大简化了ViewContorl里的代码量

在只需一行就能获得首字母分类排序后的数组

参考:http://rainbownight.blog.51cto.com/1336585/1368730

//导入 #import "NSString+PinYin.h"
//索引数组
NSArray *indexArray= [array arrayWithPinYinFirstLetterFormat];

 

主要代码

#import "ViewController.h"
#import "NSString+PinYin.h"

@interface ViewController ()<UITableViewDelegate,UITableViewDataSource,UISearchBarDelegate>

@property(nonatomic,strong) UITableView
*myTableView;
@property(nonatomic,strong) NSMutableArray
*dataArray;

@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self initDataSource];
[self createTableView];
}

 

UI及数据源

#pragma mark----CreatMyCustomTablevIew-----
- (void)createTableView
{
self.myTableView
= [[UITableView alloc] initWithFrame:CGRectMake(0,20,self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
self.myTableView.
delegate = self;
self.myTableView.dataSource
= self;
[self.myTableView registerClass:[UITableViewCell
class] forCellReuseIdentifier:@"REUSE_CELLID"];
self.myTableView.contentSize
=CGSizeMake(self.view.frame.size.width, self.view.frame.size.height*2);
[self.view addSubview:self.myTableView];
self.myTableView.sectionIndexColor
=[UIColor colorWithRed:0.10 green:0.68 blue:0.94 alpha:1.0]; self.myTableView.sectionIndexBackgroundColor=[UIColor clearColor];
[self.myTableView registerClass:[UITableViewCell
class] forCellReuseIdentifier:@"cellID"];

UISearchBar
*mSearchBar = [[UISearchBar alloc] init];
mSearchBar.
delegate = self;
mSearchBar.placeholder
= @"搜索";
[mSearchBar setAutocapitalizationType:UITextAutocapitalizationTypeNone];
[mSearchBar sizeToFit];
self.myTableView.tableHeaderView
=mSearchBar;
}

- (void)initDataSource
{
NSArray
*array = @[@"登记", @"大奔", @"周傅", @"爱德华",@"((((", @"啦文琪羊", @" s文强", @"过段时间", @"等等", @"各个", @"宵夜**", @"***", @"贝尔",@"*而结婚*", @"返回***", @"你还", @"与非门*", @"是的", @"*模块*", @"*没做*",@"俄文", @" *#咳嗽", @"6",@"fh",@"C罗",@"邓肯"];


self.dataArray
=[NSMutableArray arrayWithArray:indexArray];

[self.myTableView reloadData];
}

 

TableView相关代理

#pragma mark--- UITableViewDataSource and UITableViewDelegate Methods---
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [self.dataArray count];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if(section == 0)
{
return 1;
}
else{
NSDictionary
*dict = self.dataArray[section];
NSMutableArray
*array = dict[@"content"];
return [array count];
}
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell
*cell = [tableView dequeueReusableCellWithIdentifier:@"cellID"];
NSDictionary
*dict = self.dataArray[indexPath.section];
NSMutableArray
*array = dict[@"content"];
cell.textLabel.text
= array[indexPath.row];
cell.textLabel.textColor
= [UIColor colorWithRed:0.10 green:0.68 blue:0.94 alpha:1.0];

return cell;
}

- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
//自定义Header标题
UIView* myView = [[UIView alloc] init];
myView.backgroundColor
= [UIColor colorWithRed:0.10 green:0.68 blue:0.94 alpha:0.7];
UILabel
*titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 90, 22)];
titleLabel.textColor
=[UIColor whiteColor];

NSString
*title = self.dataArray[section][@"firstLetter"];
titleLabel.text
=title;
[myView addSubview:titleLabel];

return myView;
}

TableView索引栏相关设置

#pragma mark---tableView索引相关设置----
//添加TableView头视图标题
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
NSDictionary
*dict = self.dataArray[section];
NSString
*title = dict[@"firstLetter"];
return title;
}


//添加索引栏标题数组
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
NSMutableArray
*resultArray =[NSMutableArray arrayWithObject:UITableViewIndexSearch];
for (NSDictionary *dict in self.dataArray) {
NSString
*title = dict[@"firstLetter"];
[resultArray addObject:title];
}
return resultArray;
}


//点击索引栏标题时执行
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
{
//这里是为了指定索引index对应的是哪个section的,默认的话直接返回index就好。其他需要定制的就针对性处理
if ([title isEqualToString:UITableViewIndexSearch])
{
[tableView setContentOffset:CGPointZero animated:NO];
//tabview移至顶部
return NSNotFound;
}
else
{
return [[UILocalizedIndexedCollation currentCollation] sectionForSectionIndexTitleAtIndex:index] - 1; // -1 添加了搜索标识
}
}

 

简单实现UITableView索引功能(中英文首字母索引)(一) ByH罗

Demo 下载  http://files.cnblogs.com/files/sixindev/PinyinIndexTableview.zip