这个最典型的就是电话本,然后根据A-Z分组, 当然很多例子,不过现在发现一个很简洁易懂的:

1. 准备数据,定义一个dictionary来显示所有的内容,这个dictionary对应的value全是数组

也就是:

A -> A1, A2...

B  -> B1, B2...

...

NSMutableDictionary *sections;

2. 创建所有的Keys

BOOL found;
// Loop through the books and create our keys
for (NSDictionary *book in self.books) //self.books 就是包含NSDictionary的数组
{
NSString *c = [[book objectForKey:@"title"] substringToIndex:1]; found = NO; for (NSString *str in [self.sections allKeys])
{
if ([str isEqualToString:c])
{
found = YES;
}
} if (!found)
{
[self.sections setValue:[[NSMutableArray alloc] init] forKey:c];
}
}

3. 把Key对应的空数组填满,这个很简单应该能看懂

for (NSDictionary *book in self.books)
{
[[self.sections objectForKey:[[book objectForKey:@"title"] substringToIndex:1]] addObject:book];
}

4. 下面就排序

for (NSString *key in [self.sections allKeys])
{
[[self.sections objectForKey:key] sortUsingDescriptors:[NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"title" ascending:YES]]];
}

5. 有了上面的方法之后,就执行UITableView必须的方法

#pragma mark -
#pragma mark Table view data source - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [[self.sections allKeys] count];
} - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return [[[self.sections allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)] objectAtIndex:section];
} - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [[self.sections valueForKey:[[[self.sections allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)] objectAtIndex:section]] count];
} - (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
return [[self.sections allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
 
static NSString *CellIdentifier = @"Cell";
 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}
 
NSDictionary *book = [[self.sections valueForKey:[[[self.sections allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)] objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row];
 
cell.textLabel.text = [book objectForKey:@"title"];
cell.detailTextLabel.text = [book objectForKey:@"description"];
 
return cell;
}

差不多了,这个是我见过最简洁的例子了。

参考:http://www.icodeblog.com/2010/12/10/implementing-uitableview-sections-from-an-nsarray-of-nsdictionary-objects/

史上最简洁的UITableView Sections 展示包含NSDicionary 的NSArray的更多相关文章

  1. GitHub上史上最全的Android开源项目分类汇总 (转)

    GitHub上史上最全的Android开源项目分类汇总 标签: github android 开源 | 发表时间:2014-11-23 23:00 | 作者:u013149325 分享到: 出处:ht ...

  2. 移动端IM开发者必读(二):史上最全移动弱网络优化方法总结

    1.前言 本文接上篇<移动端IM开发者必读(一):通俗易懂,理解移动网络的“弱”和“慢”>,关于移动网络的主要特性,在上篇中已进行过详细地阐述,本文将针对上篇中提到的特性,结合我们的实践经 ...

  3. 你想找的Python资料这里全都有!没有你找不到!史上最全资料合集

    你想找的Python资料这里全都有!没有你找不到!史上最全资料合集 2017年11月15日 13:48:53 技术小百科 阅读数:1931   GitHub 上有一个 Awesome - XXX 系列 ...

  4. 史上最全面的SignalR系列教程-4、SignalR 自托管全解(使用Self-Host)-附各终端详细实例

    1.概述 通过前面几篇文章 史上最全面的SignalR系列教程-1.认识SignalR 史上最全面的SignalR系列教程-2.SignalR 实现推送功能-永久连接类实现方式 史上最全面的Signa ...

  5. Java基础面试题(史上最全、持续更新、吐血推荐)

    文章很长,建议收藏起来,慢慢读! 疯狂创客圈为小伙伴奉上以下珍贵的学习资源: 疯狂创客圈 经典图书 : <Netty Zookeeper Redis 高并发实战> 面试必备 + 大厂必备 ...

  6. spring + spring mvc + tomcat 面试题(史上最全)

    文章很长,而且持续更新,建议收藏起来,慢慢读! 高并发 发烧友社群:疯狂创客圈(总入口) 奉上以下珍贵的学习资源: 疯狂创客圈 经典图书 : 极致经典 + 社群大片好评 < Java 高并发 三 ...

  7. markdown写ppt (史上最全)

    文章很长,建议收藏起来,慢慢读! 疯狂创客圈为小伙伴奉上以下珍贵的学习资源: 疯狂创客圈 经典图书 : <Netty Zookeeper Redis 高并发实战> 面试必备 + 大厂必备 ...

  8. 史上最简单,一步集成侧滑(删除)菜单,高仿QQ、IOS。

    重要的话 开头说,not for the RecyclerView or ListView, for the Any ViewGroup. 本控件不依赖任何父布局,不是针对 RecyclerView. ...

  9. 开源框架】Android之史上最全最简单最有用的第三方开源库收集整理,有助于快速开发

    [原][开源框架]Android之史上最全最简单最有用的第三方开源库收集整理,有助于快速开发,欢迎各位... 时间 2015-01-05 10:08:18 我是程序猿,我为自己代言 原文  http: ...

随机推荐

  1. Centos7:Failed to start LSB: Bring up/down networking

    一开始配置的ifcfg-ens33 [root@cdh- ~]# cd /etc/sysconfig/network-scripts/ [root@cdh- network-scripts]# ls ...

  2. AllJoyn+Android开发案例-android跨设备调用方法

    AllJoyn+Android开发案例-android跨设备调用方法 项目须要涉及AllJoyn开源物联网框架.前面主要了解了一些AllJoyn主要的概念.像总线,总线附件,总线对象,总线接口这种概念 ...

  3. WIN10平板如何打开文件夹选项

    打开计算机,然后查看,最后可以找到选项  

  4. 树莓派3中安装JDK

    一.简介 树莓派3(Raspbian系统,下载地址:https://www.raspberrypi.org/downloads/raspbian/),安装JDK8,直接运行:apt-get insta ...

  5. 关于ř与tableau的集成---- k均值聚类

    1.利用R内置数据集iris: 2.通过Rserve 包连接tableau,服务器:localhost,默认端口6311: 3.加载数据集iris: 4.编辑字段:Cluster <span s ...

  6. Docker(二):Registry 镜像仓库

  7. 判断Android 当前版本是否为debug版本

    public static boolean isDebugVersion(Context context) { try { ApplicationInfo info = context.getAppl ...

  8. 通过nginx中转获取不到IP的问题解决

    第一步:在nginx.conf中配置反向代理时把真实IP带上,例如: server {     listen 80;     server_name  myibook.com.cn;     loca ...

  9. css table样式

    1.table样式首先设置表格边框,属性设置表格的边框是否被合并为一个单一的边框. table{ border-collapse: collapse; border-spacing: 0;} 2.固定 ...

  10. Unity3D Adam Demo的学习与研究

      1.简述 这篇文章是对Adam各种相关资料了解后进行一些精简的内容.如果你想仔细研究某个技术请跳转至unity相关页面. Adam官方页面: https://unity3d.com/cn/page ...