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开发之表视图(UITableView)
IOS开发之表视图(UITableView)的基本介绍(一) (一):UITableView的基本概念 1.在IOS开发中,表视图的应用十分广泛和普及.因此掌握表视图的用法显得非常重要.一般情况下对于 ...
- iOS开发之表视图爱上CoreData
在接触到CoreData时,感觉就是苹果封装的一个ORM.CoreData负责在Model的实体和sqllite建立关联,数据模型的实体类就相当于Java中的JavaBean, 而CoreData的功 ...
- IOS之表视图添加索引
我们要实现的效果如下. 1.修改ControlView.h,即添加变量dict,用于存储TabelView的数据源. #import <UIKit/UIKit.h> @interface ...
- iOS开发高级分享 - iOS的可折叠表视图
导言 我曾经开发过一个iphone应用程序,它显示了大量的输入,这些输入分为不同的类别,在`UITableView`...若要更改其中一个输入的值,用户按下表视图中的对应行,并在出现的单独屏幕中更改该 ...
- IOS之表视图添加搜索栏
下面是我们要实现的效果.本效果是在上一篇自定义表视图的基础上进行更改的. 1.将Search bar and search display拖动到ViewController中.不要添加Sear ...
- iOS开发——UI_swift篇&UITableView实现索引功能
UITableView实现索引功能 关于UItableView的索引在平时项目中所见不多,最多的就是跟联系人有关的界面,虽然如此,但是作为一个swift开发的程序必须知道的一个技术点,所以今天 ...
- IOS开发-UI基础-视图
//------------------------------UIWindow--------------------------// 1.UIWindow:是 UIView 的子类,用于管理.协调 ...
- iOS开发中获取视图在屏幕上显示的位置
在iOS开发中,我们会经常遇到一个问题,例如,点击一个按钮,弹出一个遮罩层,上面显示一个弹框,弹框显示的位置在按钮附近.如果这个按钮的位置相对于屏幕边缘的距离是固定的,那就容易了,可以直接写死位置.可 ...
- iOS开发小技巧 - UILabel添加中划线
iOS开发小技巧 遇到的问题: 给Label添加中划线,然后并没有效果 NSString *str = [NSString stringWithFormat:@"合计金额 ¥%.2f&quo ...
随机推荐
- 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 ...
- PHP--关于模板的原理和解析
此内容用作笔记,以备日后查看,此内容为学习李炎恢课程而来,并非自己所创,如有问题请私信~ 将PHP代码和静态HTML代码进行分离,使代码的可读性和维护性得到显著提高. 使用模板引擎: 我们所说的模板是 ...
- shareplex的安装&&起停服务(添加新用户)
一.主机环境 主从类型 系统版本 数据库版本 主机地址 主机名 源数据库 Centos6.4 X86_64 11.2.0.4.0 192.168.3.230 dbshareplex 目的数据库 C ...
- Scala中的元组
元组 元组使用()表示的数据结构 元组使用()表示的数据结构 还可以使用模式匹配访问 使用场景非常有限,用于函数返回值不止1个的情况下 看代码: /** * 元组 * @author Administ ...
- cent0s7 显卡驱动导致重启黑屏
由于 CentOS7.0 版本与前面的 CentOS6.5 及之前的版本的模式变更方法有很大 的不同,以前的版本中我们可以在vi /etc/inittab 文件中将id:5:initdefault 更 ...
- CSS3秘笈第三版涵盖HTML5学习笔记1~5章
第一部分----CSS基础知识 第1章,CSS需要的HTML HTML越简单,对搜索引擎越友好 div是块级元素,span是行内元素 <section>标签包含一组相关的内容,就像一本书中 ...
- java演示适配器(adapter)模式
为什么要使用模式: 模式是一种做事的一种方法,也即实现某个目标的途径,或者技术. adapter模式的宗旨就是,保留现有类所提供的服务,向客户提供接口,以满足客户的需求. 类适配器:客户端定义了接口并 ...
- crackme_zapline分析
[破文标题]crackme_zapline 分析 [破文作者]CloAk [作者邮箱]@qq.com [作者主页] [破解工具]OD,... [破解平台]Windows --------------- ...
- UIProgressView[进度条][一般型];UIStepper步数器][事件驱动型]
//// ViewController.m// ProgressAndSteper//// Created by hehe on 15/9/21.// Copyright (c) 2015年 ...
- maven中scope参数说明
官方说明文档地址https://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html#Depen ...