UITableView实现索引

效果图类似这样,普通的通讯录样式

在网上找到很多帖子,都没成功,最后还是官方的文档靠谱:

TableView PG

其实最关键的,就是数据源。需要把原始的数据结构,组织成二维数组,第一层数组用于分section,第二层数组是每个section中的元素。主要代码如下(清晰起见,省略无关代码):

每一行的数据结构:

@interface Member : NSObject

@property(nonatomic,copy) NSString *pk;
@property(nonatomic,copy) NSString *name;
@property NSInteger sectionNumber;

@end

首先是从数据源中读取原始数据,这时还没有分组:

NSMutableArray *membersTemp = [NSMutableArray array];

while(){
    Member *member = [[Member alloc] initWithPk:pk Name:name];
    [membersTemp addObject:member];
}

然后给每个Memer分配SectionNumber,这里用到了UILocalizedIndexedCollation类,不需要自己处理索引分组。网上很多例子都用到外部的分组方法,其实是不需要的:

UILocalizedIndexedCollation *collation = [UILocalizedIndexedCollation currentCollation];

for(Member *member in membersTemp) {
    NSInteger sect = [collation sectionForObject:member collationStringSelector:@selector(name)];
    member.sectionNumber = sect;
}

然后创建了27个数组,分别是A-Z和#的索引,数组只是初始化,还没有把Member放进去:

NSInteger highSection = [[collation sectionTitles] count];
NSMutableArray *sectionArrays = [NSMutableArray arrayWithCapacity:highSection];
for(int i = 0; i < highSection; i++) {
    NSMutableArray *sectionArray = [NSMutableArray arrayWithCapacity:1];
    [sectionArrays addObject:sectionArray];
}

然后把Member放入合适的数组:

for(Member *member in membersTemp) {
    [(NSMutableArray*)[sectionArrays objectAtIndex:member.sectionNumber] addObject:member];
}

最后,把数组排序,放到真正的members里,这样members就是一个二维数组:

for(NSMutableArray *sectionArray in sectionArrays) {
    NSArray *sortedSection = [collation sortedArrayFromArray:sectionArray collationStringSelector:@selector(name)];
    [members addObject:sortedSection];
}

经过上面的代码,members就是已经组织好的二维数组。接下来要实现TableViewDataSource的代理方法:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [members count];// 多少个section
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [[members objectAtIndex:section] count];// section的row数,会调用多次
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:[UITableViewCell reuseIdentifier]];
    if(!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:[UITableViewCell reuseIdentifier]];
    }

    Member *member = [[members objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];// 每一行
    cell.textLabel.text = member.name;
    return cell;
}

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
    return [[UILocalizedIndexedCollation currentCollation] sectionIndexTitles];// A-Z, #
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{

    if ([[members objectAtIndex:section] count] > 0) {
        return [[[UILocalizedIndexedCollation currentCollation] sectionTitles] objectAtIndex:section];// 空的数组,没有title
    }
    return nil;
}

- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
{
    return [[UILocalizedIndexedCollation currentCollation] sectionForSectionIndexTitleAtIndex:index];// 点击索引导航
}

关于这个问题,官方文档比网上任何文档都清楚

UITableView实现索引

时间: 2024-08-29 13:05:06

UITableView实现索引的相关文章

iOS开发——UI_swift篇&amp;UITableView实现索引功能

UITableView实现索引功能 像iOS中的通讯录,通过点击联系人表格右侧的字母索引,我们可以快速定位到以该字母为首字母的联系人分组. 要实现索引,我们只需要两步操作: (1)实现索引数据源代理方法 (2)响应点击索引触发的代理事件 效果图如下: 代码如下: 1 import UIKit 2 3 class ViewController: UIViewController , UITableViewDelegate, UITableViewDataSource{ 4 5 var tableV

IOS开发——UI进阶篇—UITableView,索引条,汽车数据展示案例

一.什么是UITableView 在iOS中,要实现展示列表数据,最常用的做法就是使用UITableViewUITableView继承自UIScrollView,因此支持垂直滚动,而且性能极佳 UITableView的两种样式UITableViewStylePlainUITableViewStyleGrouped 二.如何展示数据 UITableView需要一个数据源(dataSource)来显示数据 UITableView会向数据源查询一共有多少行数据以及每一行显示什么数据等 没有设置数据源的

iOS UITableView表格索引

 if ([myTableView respondsToSelector:@selector(setSectionIndexColor:)]) {         myTableView.sectionIndexColor = [UIColor redColor];         myTableView.sectionIndexBackgroundColor = [UIColor yellowColor];     } for(UIView *view in [tableView subvie

Swift - 给表格UITableView添加索引功能(快速定位)

像iOS中的通讯录,通过点击联系人表格右侧的字母索引,我们可以快速定位到以该字母为首字母的联系人分组. 要实现索引,我们只需要两步操作: (1)实现索引数据源代理方法 (2)响应点击索引触发的代理事件 效果图如下: 代码如下: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 4

UITableView 右侧索引栏 的相关设置

UITableView 也许是平常开发过程中用的最多的了,应该说每个开发者都熟悉的, 昨天新入行的朋友,问到我 tableview右侧的索引栏的字体颜色,和背景颜色怎么设置 本人也是许久没用到这些属性了,一时竟然想不起来,翻看了一下笔记,今天写出来, 希望能给朋友们一些帮助 索引栏底色 tableView.sectionIndexBackgroundColor = [UIColor blackColor]; 索引字体颜色 tableView.sectionIndexColor = [UIColo

UITableView(支持索引的分组表)

重载函数: -(NSArray *) sectionIndexTitlesForTableView: (UITableView *) tableView { //dict allKeys取出的key arr无顺序,需进行排序 NSArray *arr = [[self.words allKeys] sortedArrayUsingSelector:@selector(compare:)]; return arr; }

猫猫学IOS(十三)UI之UITableView学习(下)汽车名牌带右侧索引

猫猫分享,必须精品 素材代码地址:http://blog.csdn.net/u013357243/article/details/44727225 原文地址:http://blog.csdn.net/u013357243?viewmode=contents 先看效果图 代码 ViewController //ps:新建iOS交流学习群:304570962 可以加猫猫QQ:1764541256 或则微信znycat 让我们一起努力学习吧. 原文:http://blog.csdn.net/u0133

UITableView知识梳理须知—(一)

1.UITableView掌握 1>  设置UITableView的dataSource.delegate 2>    UITableView多组数据和单组数据的展示 3>  UITableViewCell的常见属性 4>    UITableView的性能优化(cell的循环利用) 5>  自定义Cell 2.什么是UITableView 在iOS中,要实现展示列表数据,最常用的做法就是使用UITableView.UITableView继承自UIScrollView,因此支

表格视图UITableView

(一)UITableView内部自动封装了一套复用机制.会让空闲的cell进入可重用线程池,当有新的cell出现会先去线程池中找有没有可复用的,没有才会创建.假如有100组数据,需要100个cell,但是手机上每屏只能放下10个,其实这时候只需创建11个cell就够用了.每一个数据模型就是一个cell.通过数据源方法来对每个cell进行数据设置.通过代理方法设置关于tableView的头,尾等视图设置. (二)tableView有两种样式:UITableViewStylePlain和UITabl