IOS开发之表视图添加索引

王朝学院·作者佚名  2016-08-27
窄屏简体版  字體: 小  |  中  |  大  |  超大  

我们要实现的效果如下。

1.修改ControlView.h,即添加变量dict,用于存储TabelView的数据源。

Cpp代码

#import <UIKit/UIKit.h> @interface IkrboyViewController5 : UIViewController{ NSMutableDictionary *dict; } @end

#import <UIKit/UIKit.h>

@interface IkrboyViewController5 : UIViewController{

NSMutableDictionary *dict;

}

@end

2.在ControlView.m添加如下修改

Cpp代码

- (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

Cpp代码

//分为多少个分组 - (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)

 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
© 2005- 王朝網路 版權所有 導航