一、表视图的索引目录

首先要创建一个TableView,之前有说过,这里就不详细说了(参考前面第十四篇)。

直接贴代码吧,

  1 #import "ViewController.h"
  2
  3 @interface ViewController ()<UITableViewDataSource,UITableViewDelegate>{
  4
  5     UITableView *tableView;
  6
  7     NSArray *list;//分组标题
  8     NSDictionary *dic;//每行内容
  9 }
 10
 11 @end
 12
 13 @implementation ViewController
 14
 15 - (void)viewDidLoad {
 16     [super viewDidLoad];
 17     // Do any additional setup after loading the view, typically from a nib.
 18     CGFloat width = self.view.frame.size.width;
 19     CGFloat height = self.view.frame.size.height;
 20
 21     tableView = [[UITableView alloc] initWithFrame:(CGRect){0,20,width,height}];
 22     tableView.dataSource = self;
 23     tableView.delegate = self;
 24     tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
 25     [self.view addSubview:tableView];
 26
 27     [self readySource];
 28 }
 29
 30 //在viewDidLoad方法中调用
 31 - (void)readySource{
 32
 33     dic = @{@"A":@[@"adhere", @"adaft", @"abase", @"alarm", @"apace"],
 34             @"B":@[@"babel", @"board", @"bili", @"band"],
 35             @"C":@[@"cabbages", @"crray", @"china", @"chafe", @"cocos", @"core"],
 36             @"D": @[@"dabbing", @"dacca", @"dady"],
 37             @"E": @[@"email", @"each", @"eager", @"ebook", @"enable", @"embalm", @"eman"],
 38             @"F": @[@"fear", @"faceBook", @"float", @"flour"],
 39             @"G": @[@"getter", @"gaba", @"grace", @"great", @"gracious"],
 40             @"H": @[@"header", @"haber", @"habit", @"hoard"],
 41             };
 42     list = dic.allKeys;
 43 }
 44
 45 //返回分组个数
 46 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
 47     return [list count];
 48 }
 49
 50 //返回每个分组中的行数
 51 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
 52     //获取分组
 53     NSString *key = [list objectAtIndex:section];
 54     //获取分组里面的数组
 55     NSArray *array = [dic objectForKey:key];
 56
 57     return [array count];
 58 }
 59
 60 - (UITableViewCell *)tableView:(UITableView *)TableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
 61
 62     //索引路径
 63     NSInteger section = [indexPath section];
 64     NSInteger row = [indexPath row];
 65
 66     //获取分组
 67     NSString *key = [list objectAtIndex:section];
 68
 69     //获取分组里面的数组
 70     NSArray *array = [dic objectForKey:key];
 71
 72     //建立可重用标识符
 73     static NSString *indentifier = @"UITableViewCell";
 74
 75 //    NSString *indentifier = [NSString stringWithFormat:@"UITableViewCell%ld%ld",(long)indexPath.row,(long)indexPath.section];
 76
 77     UITableViewCell *cell = [TableView dequeueReusableCellWithIdentifier:indentifier];
 78     if (!cell) {
 79         cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:indentifier];
 80     }
 81
 82     //设置其辅助样式
 83     cell.accessoryType = UITableViewCellAccessoryNone;
 84
 85     //移除所有子视图
 86     [cell.subviews enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
 87         UIView *view = (UIView*)obj;
 88         [view removeFromSuperview];
 89     }];
 90
 91     //添加新视图
 92     UILabel *title = [[UILabel alloc] initWithFrame:(CGRect){20,10,200,30}];
 93     NSString *str = [array objectAtIndex:row];
 94     title.text = str;
 95     title.font = [UIFont systemFontOfSize:20];
 96     title.textColor = [UIColor blueColor];
 97     [cell addSubview:title];
 98
 99     return cell;
100 }
101
102 //获取分组标题
103 - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
104
105     NSString *key = [list objectAtIndex:section];
106     return key;
107 }
108
109 //给TableViewCell添加索引
110 - (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{
111
112     return list;
113
114 }
115
116 //点击目录
117 - (NSInteger)tableView:(UITableView *)TableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index{
118
119 //获取所点目录对应的IndexPath值
120     NSIndexPath *selectIndexPath = [NSIndexPath indexPathForRow:0 inSection:index];
121
122 //让Table滚动到对应的indexPath位置
123     [TableView scrollToRowAtIndexPath:selectIndexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];
124     return index;
125 }
126
127 //设置TableViewCell行高
128 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
129
130     return 50;
131 }

效果图:

二、可以进行标记的表视图

首先要在- (UITableViewCell *)tableView:(UITableView *)TableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;方法中,把cell.accessoryType = UITableViewCellAccessoryNone;

 1 //点击行事件
 2 - (void)tableView:(UITableView *)TableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
 3
 4     //获取点击行的cell
 5     UITableViewCell *cell = [TableView cellForRowAtIndexPath:indexPath];
 6
 7     //如果cell已经被标记
 8     if (cell.accessoryType == UITableViewCellAccessoryCheckmark) {
 9         //取消标记
10         cell.accessoryType = UITableViewCellAccessoryNone;
11     }else
12         //反之,标记
13         cell.accessoryType = UITableViewCellAccessoryCheckmark;
14
15     //取消选中效果
16     [TableView deselectRowAtIndexPath:indexPath animated:YES];
17 }

效果图:

转载于:https://www.cnblogs.com/0320y/p/5098670.html

ios基础篇(二十六)—— UITableViewCell的分组索引与标记相关推荐

  1. python进阶记录之基础篇二十六_Python进阶记录之基础篇(十六)

    回顾 在Python进阶记录之基础篇(十五)中,我们介绍了面向对象的基本概念以及Python中类和对象的基础知识,需要重点掌握类的创建和对象的使用.今天我们继续讲一下Python中面向对象的相关知识点 ...

  2. WF4.0 基础篇 (二十六) Interop调用WF3.X的Activity

    本节主要介绍Interop调用WF3.X的Activity 本文例子下载: http://files.cnblogs.com/foundation/InteropSample.rar 本文例子说明 I ...

  3. C/C++基础讲解(二十六)之数值计算与趣味数学篇(打鱼还是晒网与怎样存钱以获取最大利息)

    C/C++基础讲解(二十六)之数值计算与趣味数学篇(打鱼还是晒网与怎样存钱以获取最大利息) 程序之美 前言 很多时候,特别是刚步入大学的学子们,对于刚刚开展的计算机课程基本上是一团迷雾,想要弄明白其中 ...

  4. 黑猫互联云计算机,黑猫评测 篇二十六:解决联想轻薄本痛点,3TB性价比NAS方案,联想个人云存储A1评测...

    黑猫评测 篇二十六:解决联想轻薄本痛点,3TB性价比NAS方案,联想个人云存储A1评测 2020-09-16 13:34:46 5点赞 10收藏 4评论 联想小新Pro 13的痛点,终于找到了完美的解 ...

  5. 计算机网络教程网线制作,图吧小白教程 篇二十六:手把手教你自制网线(夹网线水晶头)...

    图吧小白教程 篇二十六:手把手教你自制网线(夹网线水晶头) 2019-11-19 23:07:38 31点赞 309收藏 27评论 创作立场声明:咕咕咕 教程最后还是出了,不过咱现在用啥还是直接网购号 ...

  6. Vue实战篇二十六:创建动态仪表盘

    系列文章目录 Vue基础篇一:编写第一个Vue程序 Vue基础篇二:Vue组件的核心概念 Vue基础篇三:Vue的计算属性与侦听器 Vue基础篇四:Vue的生命周期(秒杀案例实战) Vue基础篇五:V ...

  7. 鸟哥的Linux私房菜(基础篇)- 第二十六章、Linux 核心编译与管理

    第二十六章.Linux核心编译与管理 最近升级日期:2009/09/18 我们说的 Linux 其实指的就是核心 (kernel) 而已.这个核心控制你主机的所有硬件并提供系统所有的功能,所以说,他重 ...

  8. 鸟哥的Linux私房菜(基础篇)- 第十六章、例行性工作排程 (crontab)

    第十六章.例行性工作排程 (crontab) 最近升级日期:2009/09/11 学习了基础篇也一阵子了,你会发现到为什么系统常常会主动的进行一些任务?这些任务到底是谁在配置工作的?如果你想要让自己设 ...

  9. ios基础篇(十二)——UINavgationController的使用(三)ToolBar

    UIToolBar存在于UINavigationController导航栏控制器中,而且默认被隐藏:设置UINavigationController的toolbarHidden属性可显示UIToolB ...

最新文章

  1. 嵌入式学习笔记之三 (uboot我来看)
  2. self.opener 和 self.parent
  3. 《中国大数据产业白皮书及百强榜单》:一览中国大数据产业发展全局
  4. C#调用浏览器的原理及实现浅析
  5. HDU 1251 统计难题 字典树/STL
  6. 防火墙术语详解(一)
  7. Windows核心编程 第八章 用户方式中线程的同步(上)
  8. POJ1195Mobile phones
  9. (chap1 网络基础知识)OSI参考模型举例
  10. 【CyberSecurityLearning 70】DC系列之DC-1渗透测试(Drupal)
  11. C#】通过遍历IFrame访问页面元素
  12. AM2320 温湿度计 单总线读取数据
  13. Linux基础命令---cpio
  14. 【渝粤教育】国家开放大学2018年秋季 0266-22T设计构成 参考试题
  15. 实现TcpIp简单传送
  16. python快乐数字怎么表达_Python经典面试题:这些面试题你会了吗?
  17. 浏览器渲染原理 记录备份
  18. 删除单向链表的最后一个节点
  19. 优秀Logo设计需要留白
  20. vmware workstation添加共享硬盘的步骤

热门文章

  1. vue-router 快速入门
  2. Android TextView内容过长加省略号,点击显示全部内容
  3. asp.net faq: 在html文件中,用js获取session
  4. EA和Take-Two游戏业巨头纷纷陷入困境
  5. 测试一个config server 服务器挂机后,集群是否能读写数据
  6. android 帧动画的使用
  7. android AppCompatEditText 自定义下划线颜色
  8. memcpy和memmove的区别
  9. ScrollView’s handy trick--android:fillViewport=quot;truequot;
  10. C++实现直接插入排序