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 下载  https://files.cnblogs.com/files/sixindev/PinyinIndexTableview.zip

 
 
 
 
 
 

简单实现UITableView索引功能(中英文首字母索引)(一) ByH罗的更多相关文章

  1. 简单实现UITableView索引功能(中英文首字母索引) (二) By HL

    简单实现UITableView索引功能(中英文首字母索引)(一) ByH罗 相关类: NSString+PinYing(获取中英文首字母)   参考上面链接 #import "ViewCon ...

  2. HBuilder+eclipse开发:使用ajax异步传值生成首字母索引

    使用ajax异步传值生成首字母索引大致有以下几个步骤: 1.服务器端使用servlet提取出数据库里的数据; 2.使用首字母工具类对数据进处理得到首字母; 3.再将首字母和数据一一对应存入json数组 ...

  3. 分享一份550多个Linux命令的文档,按照命令首字母索引排序

    输入一个命令,让我给你一个关于它的完美解释! 众所周知,Linux命令是IT人必须掌握的一个技能,有了它,我们可以部署和维护各种各样的服务和应用.但是,大部分的Linux命令我们不一定记得住,而别是各 ...

  4. PHP提取中英文首字母的方法(首字母索引)

    function Getzimu($str) { $str= iconv("UTF-8","gb2312", $str);//如果程序是gbk的,此行就要注释掉 ...

  5. 微信小程序通讯录首字母索引效果,车辆品牌选择列表

    效果图: wxml代码: <block wx:for="{{list}}"> <view class='letter' id="letter{{inde ...

  6. 做个简单的Android列表字母索引控件

    相信大家在许多App中都见到过带字母索引的界面,比如我最近看到的这个开源控件: WaveSideBar 很酷是不是?!!!如果加在例如联系人列表界面上,大大提升了用户体验. 那么这个索引控件要怎么做呢 ...

  7. iOS开发——UI_swift篇&UITableView实现索引功能

    UITableView实现索引功能     关于UItableView的索引在平时项目中所见不多,最多的就是跟联系人有关的界面,虽然如此,但是作为一个swift开发的程序必须知道的一个技术点,所以今天 ...

  8. IOS开发中实现UITableView按照首字母将集合进行检索分组

    在开发公司项目中遇到了将图书目录进行按照首字母分组排序的问题 1.在项目添加解析汉字拼音的Pinyin.h文件 /* * pinyin.c */ #define HANZI_START 19968 # ...

  9. 联系人的侧边字母索引ListView 将手机通讯录姓名通过首字母排序。

      package com.lixu.letterlistview; import java.util.ArrayList; import java.util.List; import org.apa ...

随机推荐

  1. 物联网大赛 - Android学习笔记(三)Android 事件处理

    学习目标: 了解事件处理概念 监听事件处理模型 事件与事件监听接口 实现事件监听方式 回调事件处理模型 常见的事件回调方法 Handler类功能与用法 Handler更新程序界面 一.监听概念 再用户 ...

  2. 编写Java程序,使用Swing布局管理器与常用控件,实现用户登录界面

    返回本章节 返回作业目录 需求说明: 使用Swing布局管理器与常用控件,实现用户登录界面 实现思路: 创建用户登录界面的类LoginFrame,在该类中创建无参数的构造方法,在构造方法中,设置窗体大 ...

  3. openmesh - impl - Remove Duplicated Vertices

    openmesh - impl - Remove Duplicated Vertices 关于openmesh元素删除实现的介绍参见:openmesh - src - trimesh delete a ...

  4. Linux-saltstack-3 saltstack的grains和pillar的基本使用

    @ 目录 一.简介 二.grains 1.查看客户端所有的grains项 2.查看grains的所有的项和值 3.查看某个项和值 (1)语法1: (2)语法2: 4.根据grains来匹配目标主机 例 ...

  5. .NET 微服务——CI/CD(4):避坑和一点经验

    如果你看过之前几篇文章,应该已经Jenkins成功搭建了CICD环境,但是进入正式环境会有一些坑,不注意中招的话很难受,这里总结一下,避免重复消耗精力. 后门漏洞 Jenkins有后门,这是个老问题了 ...

  6. pytest 一个测试类怎样使用多种fixture前置方法

    fixture()方法写在哪里? @pytest.fixture(scope="范围")写在conftest文件中,如下图 怎么使用fixture()呢?分为一个类中使用一个前置或 ...

  7. APP自动化测试之手机滑屏

    相信大家在安装一个APP之后,进入之前会有几个页面组成的滑屏欢迎页面,要对这个APP进行自动化测试之前,就需要实现自动滑屏,怎么实现呢?请继续往下看 滑屏分 左滑和右滑,上滑.下滑 实现的原理(左滑) ...

  8. oracle 之 while循环月份

    需求 需要跑一个数据,时间从17年5月到21年3月. 代码(简单粗暴实现) DECLARE i number; BEGIN i:= 201705; WHILE i <202104 LOOP if ...

  9. redis 加锁与解锁的详细总结,解决线程并发导致脏数据

    1.前言 对每个controller来说都是全新且单独的,原因是多线程,如果多个请求操作共有的数据,这样的并发操作会导致脏数据 怎么解决? mysql可以使用积极锁解决, 这里讲解的是redis的解决 ...

  10. 基于Jenkins+Maven+Gitea+Nexus从0到1搭建CICD环境

    在传统的单体软件架构中,软件开发.测试.运维都是以单个进程为单位. 当拆分成微服务之后,单个应用可以被拆分成多个微服务,比如用户系统,可以拆分成基本信息管理.积分管理.订单管理.用户信息管理.合同管理 ...