我们要实现的效果如下。

1.修改ControlView.h,即添加变量dict,用于存储TabelView的数据源。

  1. #import <UIKit/UIKit.h>
  2. @interface IkrboyViewController5 : UIViewController{
  3. NSMutableDictionary *dict;
  4. }
  5. @end
#import <UIKit/UIKit.h>

@interface IkrboyViewController5 : UIViewController{
NSMutableDictionary *dict;
} @end

2.在ControlView.m添加如下修改

  1. - (void)viewDidLoad
  2. {
  3. [super viewDidLoad];
  4. [self initTableViewData];
  5. // Do any additional setup after loading the view.
  6. }
  7. -(void)initTableViewData{
  8. NSBundle *bundle = [NSBundle mainBundle];
  9. NSString *plistPath = [bundle pathForResource:@"user_head" ofType:@"plist"];
  10. NSArray *dataArr = [[NSArray alloc] initWithContentsOfFile:plistPath];
  11. //将所有数据分为三组
  12. NSMutableArray *arr1 = [NSMutableArray array];
  13. NSMutableArray *arr2 = [NSMutableArray array];
  14. NSMutableArray *arr3 = [NSMutableArray array];
  15. dict = [NSMutableDictionary dictionary];
  16. [dict setObject:arr1 forKey:@"Group1"];
  17. [dict setObject:arr2 forKey:@"Group2"];
  18. [dict setObject:arr3 forKey:@"Group3"];
  19. //设置划分数据的依据,即根据index分三组,index为0-1的为第一组,2-4为第二组,5为第三组
  20. for(NSInteger index = 0; index < [dataArr count]; index++){
  21. NSDictionary *item = [dataArr objectAtIndex:index];
  22. if(index<2){
  23. [arr1 addObject:item];
  24. }
  25. else if(index>=2&&index<5){
  26. [arr2 addObject:item];
  27. }
  28. else if(index>=5){
  29. [arr3 addObject:item];
  30. }
  31. }
  32. }
- (void)viewDidLoad
{
[super viewDidLoad];
[self initTableViewData];
// Do any additional setup after loading the view.
} -(void)initTableViewData{
NSBundle *bundle = [NSBundle mainBundle];
NSString *plistPath = [bundle pathForResource:@"user_head" ofType:@"plist"];
NSArray *dataArr = [[NSArray alloc] initWithContentsOfFile:plistPath];
//将所有数据分为三组
NSMutableArray *arr1 = [NSMutableArray array];
NSMutableArray *arr2 = [NSMutableArray array];
NSMutableArray *arr3 = [NSMutableArray array]; dict = [NSMutableDictionary dictionary];
[dict setObject:arr1 forKey:@"Group1"];
[dict setObject:arr2 forKey:@"Group2"];
[dict setObject:arr3 forKey:@"Group3"]; //设置划分数据的依据,即根据index分三组,index为0-1的为第一组,2-4为第二组,5为第三组
for(NSInteger index = 0; index < [dataArr count]; index++){
NSDictionary *item = [dataArr objectAtIndex:index];
if(index<2){
[arr1 addObject:item];
}
else if(index>=2&&index<5){
[arr2 addObject:item];
}
else if(index>=5){
[arr3 addObject:item];
}
}
}

3.初始化TableView

  1. //分为多少个分组
  2. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  3. {
  4. return [[dict allKeys] count];
  5. }
  6. //每个分组的数据单元个数
  7. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  8. {
  9. switch(section)
  10. {
  11. case 0:
  12. {
  13. return [[dict objectForKey:@"Group1"] count];
  14. }
  15. case 1:
  16. {
  17. return [[dict objectForKey:@"Group2"] count];
  18. }
  19. case 2:
  20. {
  21. return [[dict objectForKey:@"Group3"] count];
  22. }
  23. }
  24. return 0;
  25. }
  26. //分组的标题,不实现下面的方法,不显示分组标题
  27. - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
  28. {
  29. //dict allKeys取出的key arr无顺序,需进行排序
  30. NSArray *arr = [[dict allKeys] sortedArrayUsingSelector:@selector(compare:)];
  31. return [arr objectAtIndex:section];
  32. }
  33. //列表右侧的索引提示,不实现下面的方法,不显示右侧索引
  34. -(NSArray *) sectionIndexTitlesForTableView: (UITableView *) tableView
  35. {
  36. //dict allKeys取出的key arr无顺序,需进行排序
  37. NSArray *arr = [[dict allKeys] sortedArrayUsingSelector:@selector(compare:)];
  38. return arr;
  39. }
  40. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  41. {
  42. static NSString *CellIdentifier = @"myTableCell";
  43. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  44. NSUInteger row = [indexPath row];
  45. NSUInteger section = [indexPath section];
  46. NSArray *arr;
  47. switch (section) {
  48. case 0:
  49. arr = [dict objectForKey:@"Group1"];
  50. break;
  51. case 1:
  52. arr = [dict objectForKey:@"Group2"];
  53. break;
  54. case 2:
  55. arr = [dict objectForKey:@"Group3"];
  56. break;
  57. default:
  58. break;
  59. }
  60. NSDictionary *rowDict = [arr objectAtIndex:row];
  61. cell.textLabel.text =  [rowDict objectForKey:@"itemName"];
  62. NSLog(@"cell.label.text =  %@",[rowDict objectForKey:@"itemName"]);
  63. NSString *imagePath = [rowDict objectForKey:@"itemImagePath"];
  64. cell.imageView.image = [UIImage imageNamed:imagePath];
  65. NSLog(@"cell.image.image  =  %@",imagePath);
  66. cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
  67. return cell;
  68. }
  69. //选中Cell响应事件
  70. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
  71. [tableView deselectRowAtIndexPath:indexPath animated:YES];//选中后的反显颜色即刻消失
  72. NSUInteger row = [indexPath row];
  73. NSUInteger section = [indexPath section];
  74. NSArray *arr;
  75. switch (section) {
  76. case 0:
  77. arr = [dict objectForKey:@"Group1"];
  78. break;
  79. case 1:
  80. arr = [dict objectForKey:@"Group2"];
  81. break;
  82. case 2:
  83. arr = [dict objectForKey:@"Group3"];
  84. break;
  85. default:
  86. break;
  87. }
  88. NSDictionary *rowDict = [arr objectAtIndex:row];
  89. NSString *userName =  [rowDict objectForKey:@"itemName"];
  90. NSLog(@"userName=%@",userName);
  91. }
//分为多少个分组
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [[dict allKeys] count];
}
//每个分组的数据单元个数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
switch(section)
{
case 0:
{
return [[dict objectForKey:@"Group1"] count];
}
case 1:
{
return [[dict objectForKey:@"Group2"] count];
}
case 2:
{
return [[dict objectForKey:@"Group3"] count];
}
}
return 0;
}
//分组的标题,不实现下面的方法,不显示分组标题
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
//dict allKeys取出的key arr无顺序,需进行排序
NSArray *arr = [[dict allKeys] sortedArrayUsingSelector:@selector(compare:)];
return [arr objectAtIndex:section];
}
//列表右侧的索引提示,不实现下面的方法,不显示右侧索引
-(NSArray *) sectionIndexTitlesForTableView: (UITableView *) tableView
{
//dict allKeys取出的key arr无顺序,需进行排序
NSArray *arr = [[dict allKeys] sortedArrayUsingSelector:@selector(compare:)];
return arr;
} - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"myTableCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; NSUInteger row = [indexPath row];
NSUInteger section = [indexPath section];
NSArray *arr; switch (section) {
case 0:
arr = [dict objectForKey:@"Group1"];
break;
case 1:
arr = [dict objectForKey:@"Group2"];
break;
case 2:
arr = [dict objectForKey:@"Group3"];
break;
default:
break;
} NSDictionary *rowDict = [arr objectAtIndex:row];
cell.textLabel.text = [rowDict objectForKey:@"itemName"];
NSLog(@"cell.label.text = %@",[rowDict objectForKey:@"itemName"]); NSString *imagePath = [rowDict objectForKey:@"itemImagePath"];
cell.imageView.image = [UIImage imageNamed:imagePath];
NSLog(@"cell.image.image = %@",imagePath); cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; return cell;
} //选中Cell响应事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[tableView deselectRowAtIndexPath:indexPath animated:YES];//选中后的反显颜色即刻消失
NSUInteger row = [indexPath row];
NSUInteger section = [indexPath section];
NSArray *arr; switch (section) {
case 0:
arr = [dict objectForKey:@"Group1"];
break;
case 1:
arr = [dict objectForKey:@"Group2"];
break;
case 2:
arr = [dict objectForKey:@"Group3"];
break;
default:
break;
} NSDictionary *rowDict = [arr objectAtIndex:row];
NSString *userName = [rowDict objectForKey:@"itemName"];
NSLog(@"userName=%@",userName);
}

IOS开发之表视图添加索引的更多相关文章

  1. IOS开发之表视图(UITableView)

    IOS开发之表视图(UITableView)的基本介绍(一) (一):UITableView的基本概念 1.在IOS开发中,表视图的应用十分广泛和普及.因此掌握表视图的用法显得非常重要.一般情况下对于 ...

  2. iOS开发之表视图爱上CoreData

    在接触到CoreData时,感觉就是苹果封装的一个ORM.CoreData负责在Model的实体和sqllite建立关联,数据模型的实体类就相当于Java中的JavaBean, 而CoreData的功 ...

  3. IOS之表视图添加索引

    我们要实现的效果如下. 1.修改ControlView.h,即添加变量dict,用于存储TabelView的数据源. #import <UIKit/UIKit.h> @interface  ...

  4. iOS开发高级分享 - iOS的可折叠表视图

    导言 我曾经开发过一个iphone应用程序,它显示了大量的输入,这些输入分为不同的类别,在`UITableView`...若要更改其中一个输入的值,用户按下表视图中的对应行,并在出现的单独屏幕中更改该 ...

  5. IOS之表视图添加搜索栏

    下面是我们要实现的效果.本效果是在上一篇自定义表视图的基础上进行更改的.     1.将Search bar and search display拖动到ViewController中.不要添加Sear ...

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

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

  7. IOS开发-UI基础-视图

    //------------------------------UIWindow--------------------------// 1.UIWindow:是 UIView 的子类,用于管理.协调 ...

  8. iOS开发中获取视图在屏幕上显示的位置

    在iOS开发中,我们会经常遇到一个问题,例如,点击一个按钮,弹出一个遮罩层,上面显示一个弹框,弹框显示的位置在按钮附近.如果这个按钮的位置相对于屏幕边缘的距离是固定的,那就容易了,可以直接写死位置.可 ...

  9. iOS开发小技巧 - UILabel添加中划线

    iOS开发小技巧 遇到的问题: 给Label添加中划线,然后并没有效果 NSString *str = [NSString stringWithFormat:@"合计金额 ¥%.2f&quo ...

随机推荐

  1. Active Session History (ASH) Performed An Emergency Flush Messages In The Alert Log

    Active Session History (ASH) Performed An Emergency Flush Messages In The Alert Log (文档 ID 1385872.1 ...

  2. PHP--关于模板的原理和解析

    此内容用作笔记,以备日后查看,此内容为学习李炎恢课程而来,并非自己所创,如有问题请私信~ 将PHP代码和静态HTML代码进行分离,使代码的可读性和维护性得到显著提高. 使用模板引擎: 我们所说的模板是 ...

  3. shareplex的安装&&起停服务(添加新用户)

    一.主机环境   主从类型 系统版本 数据库版本 主机地址 主机名 源数据库 Centos6.4 X86_64 11.2.0.4.0 192.168.3.230 dbshareplex 目的数据库 C ...

  4. Scala中的元组

    元组 元组使用()表示的数据结构 元组使用()表示的数据结构 还可以使用模式匹配访问 使用场景非常有限,用于函数返回值不止1个的情况下 看代码: /** * 元组 * @author Administ ...

  5. cent0s7 显卡驱动导致重启黑屏

    由于 CentOS7.0 版本与前面的 CentOS6.5 及之前的版本的模式变更方法有很大 的不同,以前的版本中我们可以在vi /etc/inittab 文件中将id:5:initdefault 更 ...

  6. CSS3秘笈第三版涵盖HTML5学习笔记1~5章

    第一部分----CSS基础知识 第1章,CSS需要的HTML HTML越简单,对搜索引擎越友好 div是块级元素,span是行内元素 <section>标签包含一组相关的内容,就像一本书中 ...

  7. java演示适配器(adapter)模式

    为什么要使用模式: 模式是一种做事的一种方法,也即实现某个目标的途径,或者技术. adapter模式的宗旨就是,保留现有类所提供的服务,向客户提供接口,以满足客户的需求. 类适配器:客户端定义了接口并 ...

  8. crackme_zapline分析

    [破文标题]crackme_zapline 分析 [破文作者]CloAk [作者邮箱]@qq.com [作者主页] [破解工具]OD,... [破解平台]Windows --------------- ...

  9. UIProgressView[进度条][一般型];UIStepper步数器][事件驱动型]

    ////  ViewController.m//  ProgressAndSteper////  Created by hehe on 15/9/21.//  Copyright (c) 2015年 ...

  10. maven中scope参数说明

    官方说明文档地址https://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html#Depen ...