IOS开发之表视图添加索引

我们要实现的效果如下。

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

Cpp代码  

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

@interface IkrboyViewController5 : UIViewController{
    NSMutableDictionary *dict;
}

@end

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

Cpp代码  

  1. - (void)viewDidLoad
  2. {
  3. [super viewDidLoad];
  4. [self initTableViewData];
  5. // Do any additional setup after loading the view.
  6. }
  7. -(void)initTableViewData{
  8. NSBundle *bundle = [NSBundle mainBundle];
  9. NSString *plistPath = [bundle pathForResource:@"user_head" ofType:@"plist"];
  10. NSArray *dataArr = [[NSArray alloc] initWithContentsOfFile:plistPath];
  11. //将所有数据分为三组
  12. NSMutableArray *arr1 = [NSMutableArray array];
  13. NSMutableArray *arr2 = [NSMutableArray array];
  14. NSMutableArray *arr3 = [NSMutableArray array];
  15. dict = [NSMutableDictionary dictionary];
  16. [dict setObject:arr1 forKey:@"Group1"];
  17. [dict setObject:arr2 forKey:@"Group2"];
  18. [dict setObject:arr3 forKey:@"Group3"];
  19. //设置划分数据的依据,即根据index分三组,index为0-1的为第一组,2-4为第二组,5为第三组
  20. for(NSInteger index = 0; index < [dataArr count]; index++){
  21. NSDictionary *item = [dataArr objectAtIndex:index];
  22. if(index<2){
  23. [arr1 addObject:item];
  24. }
  25. else if(index>=2&&index<5){
  26. [arr2 addObject:item];
  27. }
  28. else if(index>=5){
  29. [arr3 addObject:item];
  30. }
  31. }
  32. }
- (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代码  

  1. //分为多少个分组
  2. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  3. {
  4. return [[dict allKeys] count];
  5. }
  6. //每个分组的数据单元个数
  7. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  8. {
  9. switch(section)
  10. {
  11. case 0:
  12. {
  13. return [[dict objectForKey:@"Group1"] count];
  14. }
  15. case 1:
  16. {
  17. return [[dict objectForKey:@"Group2"] count];
  18. }
  19. case 2:
  20. {
  21. return [[dict objectForKey:@"Group3"] count];
  22. }
  23. }
  24. return 0;
  25. }
  26. //分组的标题,不实现下面的方法,不显示分组标题
  27. - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
  28. {
  29. //dict allKeys取出的key arr无顺序,需进行排序
  30. NSArray *arr = [[dict allKeys] sortedArrayUsingSelector:@selector(compare:)];
  31. return [arr objectAtIndex:section];
  32. }
  33. //列表右侧的索引提示,不实现下面的方法,不显示右侧索引
  34. -(NSArray *) sectionIndexTitlesForTableView: (UITableView *) tableView
  35. {
  36. //dict allKeys取出的key arr无顺序,需进行排序
  37. NSArray *arr = [[dict allKeys] sortedArrayUsingSelector:@selector(compare:)];
  38. return arr;
  39. }
  40. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  41. {
  42. static NSString *CellIdentifier = @"myTableCell";
  43. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  44. NSUInteger row = [indexPath row];
  45. NSUInteger section = [indexPath section];
  46. NSArray *arr;
  47. switch (section) {
  48. case 0:
  49. arr = [dict objectForKey:@"Group1"];
  50. break;
  51. case 1:
  52. arr = [dict objectForKey:@"Group2"];
  53. break;
  54. case 2:
  55. arr = [dict objectForKey:@"Group3"];
  56. break;
  57. default:
  58. break;
  59. }
  60. NSDictionary *rowDict = [arr objectAtIndex:row];
  61. cell.textLabel.text =  [rowDict objectForKey:@"itemName"];
  62. NSLog(@"cell.label.text =  %@",[rowDict objectForKey:@"itemName"]);
  63. NSString *imagePath = [rowDict objectForKey:@"itemImagePath"];
  64. cell.imageView.image = [UIImage imageNamed:imagePath];
  65. NSLog(@"cell.image.image  =  %@",imagePath);
  66. cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
  67. return cell;
  68. }
  69. //选中Cell响应事件
  70. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
  71. [tableView deselectRowAtIndexPath:indexPath animated:YES];//选中后的反显颜色即刻消失
  72. NSUInteger row = [indexPath row];
  73. NSUInteger section = [indexPath section];
  74. NSArray *arr;
  75. switch (section) {
  76. case 0:
  77. arr = [dict objectForKey:@"Group1"];
  78. break;
  79. case 1:
  80. arr = [dict objectForKey:@"Group2"];
  81. break;
  82. case 2:
  83. arr = [dict objectForKey:@"Group3"];
  84. break;
  85. default:
  86. break;
  87. }
  88. NSDictionary *rowDict = [arr objectAtIndex:row];
  89. NSString *userName =  [rowDict objectForKey:@"itemName"];
  90. NSLog(@"userName=%@",userName);
  91. }
//分为多少个分组
- (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);
}
时间: 2024-08-02 06:02:00

IOS开发之表视图添加索引的相关文章

IOS开发之表视图(UITableView)

IOS开发之表视图(UITableView)的基本介绍(一) (一):UITableView的基本概念 1.在IOS开发中,表视图的应用十分广泛和普及.因此掌握表视图的用法显得非常重要.一般情况下对于数据的展示 我们都会选择表视图,比如通讯录和一些数据列表. 2.我们可以选择创建表视图也可以创建表视图控制器. (二)UITableView基本样式如下(1:UITableViewStylePlain(普通表视图),2:UITableViewStyleGroup(分组表视图)): (三)UITabl

IOS开发之表视图爱上CoreData

在接触到CoreData时,感觉就是苹果封装的一个ORM.CoreData负责在Model的实体和sqllite建立关联,数据模型的实体类就相当于Java中的JavaBean, 而CoreData的功能和JavaEE中的Hibernate的功能类似,最基本是两者都有通过对实体的操作来实现对数据库的CURD操作.CoreData中的上下文(managedObjectContext)就相当于Hibernate中的session对象, CoreData中的save操作就和Hibernate中的comm

ios开发(表视图)

1.在对象库中拖拉tableview至场景中.再拖拉tableviewcell作为原型单元格,注意要设置原型单元格标识符以备后面代码实现使用 2.设置tableVIEW属性(content:Dynamic Prototypes style:grouped),原型单元格属性(style:basic,image,identifier标识符,identation:1) 3.将tableview的输出口delegate和datasource链接至viewcontroller 4.实现逻辑(书本例子) 在

IOS之表视图添加索引

我们要实现的效果如下. 1.修改ControlView.h,即添加变量dict,用于存储TabelView的数据源. Cpp代码   #import <UIKit/UIKit.h> @interface IkrboyViewController5 : UIViewController{ NSMutableDictionary *dict; } @end #import <UIKit/UIKit.h> @interface IkrboyViewController5 : UIView

iOS开发- 自定义遮罩视图(引导, 功能说明)源码+解析

iOS开发- 自定义遮罩视图(引导, 功能说明)源码+解析 我们平时使用App的时候, 经常在第一次使用的时候, 会有类似"新手教程"之类的东西, 来引导我们应该如何使用这个App. 但是这个"新手教程"不同于常规的引导页(引导页指第一次打开App时候, 弹出的那种介绍视图. 他是静态的, 不需要与用户交互, 可以直接一页页翻, 或者直接跳过.)所谓的"新手教程", 就是按照App的提示, 一步步跟着完成. 那这个"新手教程"

iOS开发项目篇—32添加上拉刷新数据

iOS开发项目篇—32添加上拉刷新数据 一.简单说明 图片示意 思路:可以自定义一个view(示意xib),在view中添加一个label和菊花,指示状态.把这个view设置为tableView的底部视图. 二.实现过程 1.新建一个类和xib,关联 (1)创建一个类,让其继承自UIView (2)创建一个xib文件,用来定义上拉提示框 (3)定义的xib文件,把类和xib文件进行关联 2.实现代码: YYlaodStatusesFooter.h文件 1 // 2 // YYlaodStatus

iOS 开发之 为UIButton添加类别方法加载网络图片

iOS 开发之 为UIButton添加类别方法加载网络图片 使用GCD线程队列实现 工程如下: UIButton+WebCache.h #import <UIKit/UIKit.h> // 为Button添加类别方法 @interface UIButton (WebCache) - (void)xr_setButtonImageWithUrl:(NSString *)urlStr; @end UIButton+WebCache.m #import "UIButton+WebCache

iOS开发项目篇—04添加导航栏的按钮

iOS开发项目篇—04添加导航栏的按钮 一.设置导航栏的按钮 要求实现的效果:             说明:默认状态下和高亮状态下的图片是不一样的. 按钮的图片需要设置默认状态和高亮状态时的显示,系统了提供的下面方法 viewController.navigationItem.leftBarButtonItem=[UIBarButtonItem alloc]initWithImage:<#(UIImage *)#> style:<#(UIBarButtonItemStyle)#>

iOS开发项目篇—02添加子控制器以及项目分层

iOS开发项目篇—02添加子控制器以及项目分层 一.添加子控制器 1.设置根控制器(自定义) 说明:分析新浪微博应用,观察其整体建构层次.而系统的控制器不能满足项目开发的需求,这里把项目中原有的控制器删除. 自己定义一个TabBarViewController类.让这个类作为window窗口的根控制器. YYAppDelegate.m文件代码: 1 #import "YYAppDelegate.h" 2 #import "YYTabBarViewController.h&qu