简单实现UITableView索引功能(中英文首字母索引)(一) ByH罗
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 添加了搜索标识 } }
Demo 下载 http://files.cnblogs.com/files/sixindev/PinyinIndexTableview.zip
转载于:https://www.cnblogs.com/sixindev/p/4504578.html
简单实现UITableView索引功能(中英文首字母索引)(一) ByH罗相关推荐
- android 通讯录 首字母索引,android仿微信通讯录搜索(匹配拼音,字母,索引位置标记颜色)...
前言: 仿微信通讯录搜索功能,通过汉字或拼音首字母找到匹配的联系人并显示匹配的位置 一:先看效果图 字母索引 搜索匹配 二:功能分析 1:汉字转拼音 通讯录汉字转拼音(首个字符当考虑姓氏多音字), 现 ...
- php 提取英文,PHP实现提取中英文首字母的方法
本文给大家分享的是使用php简单实现的提取中英文字符的首字母的方法和示例,十分的使用,有需要的小伙伴可以参考下. PHP实现获取中英文按照首字母/** * */ function getfirstch ...
- 微信小程序|使用小程序实现通讯录功能,首字母快捷导航
基于微信小程序的通讯录功能,通过右侧首字母实现快捷导航效果 前言:使用快速导航栏迅速找到关键信息,给程序的使用上增加一些技巧. 目录 一.功能介绍 二.小程序效果图 三.实现步骤 四.完整代码 一.功 ...
- 爽啊!写了一个网页:首字母索引的单词(十分垃圾,简单的不得了)
效果图: 无 GIF太难了 Html <!DOCTYPE html> <html lang="en"> <head><meta chars ...
- 实现自己的首字母索引导航列表(二)——拼音首字母分类列表
文章目录 1.创建多类型布局列表 1.1 定义列表加载数据格式 1.1 创建适配器 1.2 添加假数据展示列表 2.对数据源进行首字母拼音分组 3.添加分割线 目标效果如下. 实现步骤: 创建 ...
- android 字母索引三方,Android ListView字母索引(仿微信通讯录列表)
布局代码 xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_paren ...
- iOS searchbar实现汉字更具拼音首字母排序
最近在做一个地图相关的应用,涉及到全国城市的切换,于是打算把这个记录下来 用到的主要东西有:百度的全国城市列表,txt格式,jasonkit数据解析第三方,tableview,searchabar,汉 ...
- 联合索引会创建几个索引_联合索引在B+Tree上的存储结构及数据查找方式
来源:SegmentFault 思否社区作者:木子雷 前言: 本篇文章主要是阐述下 联合索引 在 B+Tree 上的实际存储结构. 本文主要讲解的内容有: 联合索引在B+树上的存储结构 联合索引的查找 ...
- 联合索引会创建几个索引_联合索引在B+树上的存储结构及数据查找方式
能坚持别人不能坚持的,才能拥有别人未曾拥有的. 关注编程大道公众号,让我们一同坚持心中所想,一起成长!! 原文首发于该公号,欢迎关注 引言 上一篇文章<MySQL索引那些事>主要讲了MyS ...
最新文章
- Python脚本:聚类小分子数据集
- laravel 发送带附件的邮件
- 数据管理的智能趋势(2):如何实现高效的数据管理
- 2013年工作中遇到的20个问题:181-200
- ping: sendto: Network is unreachable
- 东北全面放开生育,数据揭秘人口和GDP和房价间联系
- 创建自定义Widgets小部件扩展
- [论文笔记]CVPR2017_Joint Detection and Identification Feature Learning for Person Search
- .NET Core with 微服务 - Consul 配置中心
- CentOS 7 搭建RAP2r Api文档管理系统
- chrome charset使用_使用JWT保护你的Spring Boot应用 Spring Security实战
- 基于javaweb房屋租赁系统设计与实现
- win10下安装Jekyll(rubyinstaller-devkit)
- Django实战: Python爬虫爬取链家上海二手房信息,存入数据库并在前端显示
- 从 0 到 1,开发一个智能问答机器人
- 在IGBT的开启过程中,IGBT的电压降低,电流上升,在IGBT的关断过程中IGBT的电压上升,电流下降,在一段时间内,电压和电流均不为0,由于功率等于电压乘以电流,即P=U×I,因此将产生损耗,开
- win10注册ocx控件的步骤(包含错误处理方法0x80040200)
- FMEA和HAZOP在煤气柜风险分析综合应用
- 做好项目成本核算需要注意哪些事项
- 机器学习之---文本分析(jieba分词和词云绘制)
热门文章
- recover 没有捕获异常_defer + recover 捕获所有异常
- python dataframe 列_python pandas库中DataFrame对行和列的操作实例讲解
- Vue项目中 css样式的作用域(深度作用选择器)
- 一、第一个注解的 SpringMVC 程序
- 天池 在线编程 最长AB子串(哈希)
- LeetCode 1662. 检查两个字符串数组是否相等
- LeetCode 1131. 绝对值表达式的最大值(数学 绝对值展开)
- LeetCode MySQL 178. 分数排名(dense_rank连续排名)
- LeetCode 433. 最小基因变化(广度优先搜索)
- LeetCode 537. 复数乘法