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 ...
随机推荐
- CentOS(二)--初识linux的一些常用命令
linux命令是对Linux系统进行管理的命令.对于Linux系统来说,无论是中央处理器.内存.磁盘驱动器.键盘.鼠标,还是用户等都是文件,Linux系统管理的命令是它正常运行的核心,与之前的DOS命 ...
- ganymed-ssh2使用
通过maven库获取ganymed-ssh2-262.jar,这是一个实现了ssh2协议的工具包,可以远程连接linux机器,执行命令,有些工作全靠它了 示例代码如下: <!--首先要建立连接, ...
- 逆向+两次bfs(UVA 1599)
为什么都说简单好想咧.坦白从宽看了人家的代码,涨了好多姿势,, http://blog.csdn.net/u013382399/article/details/38227917 被一个细节坑了.. 2 ...
- Scala官方作弊条
Scala官方作弊条请参考:http://docs.scala-lang.org/cheatsheets/
- Oracle创建表空间、新建用户和授权
通过pl/sql以sys用户登录到Oracle数据库上,然后执行菜单:文件/新建/命令窗口 ,打开一个命令窗口然后在该命令窗口中执行脚本创建和删除表空间 . 1.创建表空间 格式: create t ...
- C#基础性问题
解决方案.项目.类之间的关系: 一个解决方案可以包含多个项目.一个项目可以包含多个类 解决方案:公司 项目:部门 类:员工 .sln:解决方案文件,里面包含着整个解决方案的信息,可以双击运行. .cs ...
- MyBatis(3.2.3) - Passing multiple input parameters
MyBatis's mapped statements have the parameterType attribute to specify the type of input parameter. ...
- Sql Server关于text类型的替换
UPDATE Store SET Body=replace(convert(varchar(8000),Body),'http://120.89.46.68:8007','')
- Oracle 12c Dataguard 数据库恢复
http://allthingsoracle.com/rolling-forward-a-physical-standby-database-using-the-recover-command/ 当主 ...
- 第四十三篇、利用NSProxy解决NSTimer内存泄漏问题
问题描述: 用NSTimer来实现每隔一定时间执行制定的任务,例如最常见的广告轮播图.如果我们在 timerWithTimeInterval:1 target:self 中指定target为当前控制器 ...