我们要实现的效果如下。

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之表视图添加搜索栏

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

  2. IOS开发之表视图添加索引

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

  3. IOS开发-表视图LV3导航控制器

    学到这里感觉有点难了,其实这篇文章再草稿箱里放了好久了~ 最近对于学习的热情下降了.这不行-抓紧学习走起! 在这一章节的学习中主要针对导航控制器及表视图来建立多视图的应用, 首先要了解一些概念-- 1 ...

  4. 数据可视化之DAX篇(七) Power BI中用DAX生成的表如何添加索引列?

    ​https://zhuanlan.zhihu.com/p/74732578 来源于知识星球中一个星友的问题,使用DAX在PowerBI中新建了一个表,如何为这个表添加索引列呢? 假如数据模型中只有一 ...

  5. iOS开发-表视图的使用

    // // ViewController.m // Simple Table // // Created by Jierism on 16/7/20. // Copyright © 2016年 Jie ...

  6. psql-09表:视图和索引

    视图 由查询语句定义的虚拟表;从视图中看到的数据可能来自数据库中的一张或多张表,也可能来自外部; 使用视图的原因一般有: 使复制的查询易于理解和使用; 安全原因; 表一些函数返回的结果映射成视图; 一 ...

  7. 查看Oracle当前用户下的信息(用户,表视图,索引,表空间,同义词,存储过程函数,约束条件)

    0.表空间 SQL>select username,default_tablespace from user_users; 查看当前用户的角色 SQL>select * from user ...

  8. ios中为视图添加圆角

    1.使用 layer设置指定圆角 或者设定一个或几个圆角 码修 关注 2017.04.20 19:03* 字数 107 阅读 656评论 0喜欢 0 由于项目中需要给按钮左下 和左上加圆角,我司可爱的 ...

  9. IOS之表视图单元格删除、移动及插入

    1.实现单元格的删除,实现效果如下 - (void)viewDidLoad { [super viewDidLoad]; //设置导航栏 self.editButtonItem.title = @&q ...

随机推荐

  1. Android四大组件小结

    Android四大组件分别为activity.service.content provider.broadcast receiver. 一.android四大组件详解 1.activity (1)一个 ...

  2. Linux选型:开源不是免费 首选红帽和SUSE

    首发:http://tech.it168.com/a2014/0324/1606/000001606245.shtml 企业级服务器系统选型报告:http://www.it168.com/redian ...

  3. Windows Socket网络编程-2016.01.07

    在使用WSAEventSelect的套接字模型中,遇到了WSAEventSelect返回10038的错误,在定位解决的过程中,简单记录一些定位解决的手段摘要. 使用windows的错误帮助信息,使用命 ...

  4. storm学习-storm入门

    超好资料: 英文:https://github.com/xetorthio/getting-started-with-storm/blob/master/ch03Topologies.asc 中文:h ...

  5. grep,awk和sed的常用命令和语法

    Grep的常用命令语法 1. 双引号引用和单引号引用在g r e p命令中输入字符串参数时,最好将其用双引号括起来.例如:“m y s t r i n g”.这样做有两个原因,一是以防被误解为 s h ...

  6. 完成了server和client的框架设计

    界面暂且也不搞.先把框架搭建起来.

  7. 浅谈Bootstrap自适应功能在Web开发中的应用

    随着移动端市场的强势崛起,web的开发也变得愈发复杂,对于个体开发者来说,自己开发的网站,在电脑.手机.Pad等上面都要有正常的显示以及良好的用户体验.如果每次都要自己去调整网页去匹配各个不同的客户端 ...

  8. ping通网关 ping不能外网  DNS无法解析

       ###ping通网关 ping不能外网  DNS无法解析 客户上不了网 DNS解析不了  首先登陆机器 先查看IP  然后看dns是否正常 然后测试ping网关  ping外网 nslookup ...

  9. 使用nodejs引用socket.io做聊天室

    Server: var app = require('http').createServer(handler) , io = require('socket.io').listen(app) , fs ...

  10. mysql给root开启远程访问权限,修改root密码

    1.MySql-Server 出于安全方面考虑只允许本机(localhost, 127.0.0.1)来连接访问. 这对于 Web-Server 与 MySql-Server 都在同一台服务器上的网站架 ...