「ios開発」iOS Table Viewにインデックスを追加する方法

1.ControlView.hを編集、dict変数を追加して、TableViewのデータを保存
#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];
//全てのデータを3つのグループに分割される
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″];

//グループ1–index 0-1 グループ2–index2-4 グループ3–index 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
{

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);
}

IOS

Posted by arkgame