国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

IOS之表視圖添加索引

2019-11-14 19:42:09
字體:
來源:轉載
供稿:網友

我們要實現的效果如下。

   

 

1.修改ControlView.h,即添加變量dict,用于存儲TabelView的數據源。

Cpp代碼 復制代碼 收藏代碼
  1. #import <UIKit/UIKit.h>  
  2.   
  3. @interface IkrboyViewController5 : UIViewController{  
  4.     NSMutableDictionary *dict;  
  5. }  
  6.   
  7. @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.   
  8. -(void)initTableViewData{  
  9.     NSBundle *bundle = [NSBundle mainBundle];  
  10.     NSString *plistPath = [bundle pathForResource:@"user_head" ofType:@"plist"];  
  11.     NSArray *dataArr = [[NSArray alloc] initWithContentsOfFile:plistPath];  
  12.     //將所有數據分為三組  
  13.     NSMutableArray *arr1 = [NSMutableArray array];  
  14.     NSMutableArray *arr2 = [NSMutableArray array];  
  15.     NSMutableArray *arr3 = [NSMutableArray array];  
  16.       
  17.     dict = [NSMutableDictionary dictionary];  
  18.     [dict setObject:arr1 forKey:@"Group1"];  
  19.     [dict setObject:arr2 forKey:@"Group2"];  
  20.     [dict setObject:arr3 forKey:@"Group3"];  
  21.       
  22.     //設置劃分數據的依據,即根據index分三組,index為0-1的為第一組,2-4為第二組,5為第三組  
  23.     for(NSInteger index = 0; index < [dataArr count]; index++){  
  24.         NSDictionary *item = [dataArr objectAtIndex:index];  
  25.         if(index<2){  
  26.             [arr1 addObject:item];  
  27.         }  
  28.         else if(index>=2&&index<5){  
  29.             [arr2 addObject:item];  
  30.         }  
  31.         else if(index>=5){  
  32.             [arr3 addObject:item];  
  33.         }  
  34.     }  
  35. }  
- (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.   
  41. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath  
  42. {  
  43.     static NSString *CellIdentifier = @"myTableCell";  
  44.     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];  
  45.       
  46.     NSUInteger row = [indexPath row];  
  47.     NSUInteger section = [indexPath section];  
  48.     NSArray *arr;  
  49.       
  50.     switch (section) {  
  51.         case 0:  
  52.             arr = [dict objectForKey:@"Group1"];  
  53.             break;  
  54.         case 1:  
  55.             arr = [dict objectForKey:@"Group2"];  
  56.             break;  
  57.         case 2:  
  58.             arr = [dict objectForKey:@"Group3"];  
  59.             break;  
  60.         default:  
  61.             break;  
  62.     }  
  63.       
  64.     NSDictionary *rowDict = [arr objectAtIndex:row];  
  65.     cell.textLabel.text =  [rowDict objectForKey:@"itemName"];  
  66.     NSLog(@"cell.label.text =  %@",[rowDict objectForKey:@"itemName"]);  
  67.       
  68.     NSString *imagePath = [rowDict objectForKey:@"itemImagePath"];  
  69.     cell.imageView.image = [UIImage imageNamed:imagePath];  
  70.     NSLog(@"cell.image.image  =  %@",imagePath);  
  71.       
  72.     cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;  
  73.       
  74.     return cell;  
  75. }  
  76.   
  77. //選中Cell響應事件  
  78. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{  
  79.     [tableView deselectRowAtIndexPath:indexPath animated:YES];//選中后的反顯顏色即刻消失  
  80.     NSUInteger row = [indexPath row];  
  81.     NSUInteger section = [indexPath section];  
  82.     NSArray *arr;  
  83.       
  84.     switch (section) {  
  85.         case 0:  
  86.             arr = [dict objectForKey:@"Group1"];  
  87.             break;  
  88.         case 1:  
  89.             arr = [dict objectForKey:@"Group2"];  
  90.             break;  
  91.         case 2:  
  92.             arr = [dict objectForKey:@"Group3"];  
  93.             break;  
  94.         default:  
  95.             break;  
  96.     }  
  97.       
  98.     NSDictionary *rowDict = [arr objectAtIndex:row];  
  99.     NSString *userName =  [rowDict objectForKey:@"itemName"];  
  100.     NSLog(@"userName=%@",userName);  
  101. }  
//分為多少個分組- (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);}

 


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 房产| 平武县| 保亭| 察哈| 丰县| 宁城县| 铁力市| 东兰县| 宣化县| 铁岭县| 天台县| 灵宝市| 怀柔区| 垣曲县| 长治市| 巍山| 独山县| 红桥区| 满洲里市| 科技| 铁岭市| 黄陵县| 安阳县| 策勒县| 霍林郭勒市| 屏南县| 旬阳县| 遵化市| 永川市| 双鸭山市| 泊头市| 高邮市| 平和县| 临安市| 兴和县| 儋州市| 和顺县| 桦南县| 广宗县| 平利县| 儋州市|