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 ...
随机推荐
- CF Anya and Ghosts (贪心)
Anya and Ghosts time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...
- HTML5自带的原生定位
使用谷歌的,与百度有偏差,一般不推荐使用 一.window.navigator.geolocation 验证浏览器是否支持 if (window.navigator.geolocation) { ...
- Java Concurrency - ThreadLocal, 本地线程变量
共享数据是多线程应用最常见的问题之一,但有时我们需要为每个线程保存一份独立的变量.Java API 提供了 ThreadLocal 来解决这个问题. 一个 ThreadLocal 作用的例子: imp ...
- As,is含义?using 语句
Is:检查对象是否与给定的类型兼容.例如,下面的代码可以确定MyObject类型的一个实例,或者对象是否从MyObject派生的一个类型: if(obj is MyObject){} ...
- jQuery之手势密码
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- C盘空间不足,释放C盘空间
最近电脑总是特别卡,后来发现C盘空间严重不足,只剩下几十兆,以前最严重的时候是剩下0kb可以,怎一个惨字了得... 我所知道的C盘空间不足会导致的几个主要问题有: 1)拷贝大文件会失败.因为拷贝和剪切 ...
- Source Insight 技巧总结
以下文章转载自网络:http://blog.csdn.net/junjie319/article/details/6910992 http://www.cnblogs.com/bluestorm/ar ...
- STAD Parameters
STAD related parameters: 1. stat/file -- define the file name of the actual STAD content – defaul ...
- [java学习笔记]java语言基础概述之运算符&程序流程控制&for循环嵌套
一.运算符 算数运算符 +,-,*,/,%,++,-- 1.+,-,*,/ 整数除以整数的结果为舍弃了小数部分的整数. 2.%:取余 %左右都为正数,左边小于右边结果为左边值,如,4%5=4 %左边大 ...
- linux下golang的配置
linux下golang的配置 之前开发golang一直在windows下,今天在linux下试了一下 ,遇到一些梗,比如go 找不到 sync包.花了一小时全部解决,把过程记录一下. 安装 go 我 ...