我們要實現的效果如下。
1.修改ControlView.h,即添加變量dict,用于存儲TabelView的數據源。
#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]; } }}
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);}
新聞熱點
疑難解答