IOS之表视图添加索引
我们要实现的效果如下。
1.修改ControlView.h,即添加变量dict,用于存储TabelView的数据源。
- #import <UIKit/UIKit.h>
- @interface IkrboyViewController5 : UIViewController{
- NSMutableDictionary *dict;
- }
- @end
#import <UIKit/UIKit.h> @interface IkrboyViewController5 : UIViewController{
NSMutableDictionary *dict;
} @end
2.在ControlView.m添加如下修改
- - (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];
- }
- }
- }
- (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
- //分为多少个分组
- - (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);
- }
//分为多少个分组
- (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);
}
- UITableView.zip (22.2 KB)
IOS之表视图添加索引的更多相关文章
- IOS之表视图添加搜索栏
下面是我们要实现的效果.本效果是在上一篇自定义表视图的基础上进行更改的. 1.将Search bar and search display拖动到ViewController中.不要添加Sear ...
- IOS开发之表视图添加索引
我们要实现的效果如下. 1.修改ControlView.h,即添加变量dict,用于存储TabelView的数据源. #import <UIKit/UIKit.h> @interface ...
- IOS开发-表视图LV3导航控制器
学到这里感觉有点难了,其实这篇文章再草稿箱里放了好久了~ 最近对于学习的热情下降了.这不行-抓紧学习走起! 在这一章节的学习中主要针对导航控制器及表视图来建立多视图的应用, 首先要了解一些概念-- 1 ...
- 数据可视化之DAX篇(七) Power BI中用DAX生成的表如何添加索引列?
https://zhuanlan.zhihu.com/p/74732578 来源于知识星球中一个星友的问题,使用DAX在PowerBI中新建了一个表,如何为这个表添加索引列呢? 假如数据模型中只有一 ...
- iOS开发-表视图的使用
// // ViewController.m // Simple Table // // Created by Jierism on 16/7/20. // Copyright © 2016年 Jie ...
- psql-09表:视图和索引
视图 由查询语句定义的虚拟表;从视图中看到的数据可能来自数据库中的一张或多张表,也可能来自外部; 使用视图的原因一般有: 使复制的查询易于理解和使用; 安全原因; 表一些函数返回的结果映射成视图; 一 ...
- 查看Oracle当前用户下的信息(用户,表视图,索引,表空间,同义词,存储过程函数,约束条件)
0.表空间 SQL>select username,default_tablespace from user_users; 查看当前用户的角色 SQL>select * from user ...
- ios中为视图添加圆角
1.使用 layer设置指定圆角 或者设定一个或几个圆角 码修 关注 2017.04.20 19:03* 字数 107 阅读 656评论 0喜欢 0 由于项目中需要给按钮左下 和左上加圆角,我司可爱的 ...
- IOS之表视图单元格删除、移动及插入
1.实现单元格的删除,实现效果如下 - (void)viewDidLoad { [super viewDidLoad]; //设置导航栏 self.editButtonItem.title = @&q ...
随机推荐
- jquery学习记录
1.选择器实例 语法 描述 $(this) 当前 HTML 元素 $("p") 所有 <p> 元素 $("p.intro") 所有 class=&q ...
- oracle中,行转列函数wm_concat()结果有长度限制,重写该函数解决
--Type CREATE OR REPLACE TYPE zh_concat_im AUTHID CURRENT_USER AS OBJECT ( CURR_STR clob, STATIC FUN ...
- namespace的用法
C++中采用的是单一的全局变量命名空间.在这单一的空间中,如果有两个变量或函数的名字完全相同,就会出现冲突.当然,你也可以使用不同的名字,但有时我们并不知道另一个变量也使用完全相同的名字:有时为了程序 ...
- HttpClient入门
HttpClient入门 HttpClient 是 Apache Jakarta Common 下的子项目,可以用来提供高效的.最新的.功能丰富的支持 HTTP 协议的客户端编程工具包,并且它支持 H ...
- SqlServer 事务和异常处理示例
BEGIN TRANSACTION--开始事务 DECLARE @errorSun INT --定义错误计数器SET @errorSun=0 --没错为0 UPDATE dbo.Test SET te ...
- typedef 及其与struct的结合使用
//相当于为现有类型创建一个别名,或称类型别名. //整形等 typedef int size; //字符数组 ]; ];//=> typedef ]; Line text, secondlin ...
- asp.net身份认证
在网上看到几篇比较好的文章很详细讲解了Form.Membership.以及Identity身份认证 Form身份认证: http://www.cnblogs.com/fish-li/archive/2 ...
- java编程思想第四版中 net.mindview.util包
操作系统: win8.1 编译环境 JDK1.6 编辑器 notepad++ 第48页练习8 1 下载相应程序包 Thinking in Java 4ed - CODE 2 设置相应的CLASSPAT ...
- 软件工程 speedsnail 第二次冲刺6
20150523 完成任务:碰撞墙壁,或线身体翻转: 遇到问题: 问题1 身体翻转与帧数冲突 解决1 运用循环嵌套解决 明日任务: 蜗牛碰到线后速度方向的调整
- mariadb的explain分析及InnoDB存储引擎
id: 当前查询语句中,每个SELECT语句的编号, id: 1 表示简单类型的查询 复杂类型的查询有三种:简单子查询,用于FROM中的子查询,联合查询:UNION 注意:UNION查询的分 ...