これで、テーブルビューを含むpopoverControllerを作成しました。また、テーブルビューに表示されるデータを作成しました。ただし、テーブルは常に空です。PS(デリゲートとデータソースが設定され、デリゲートメソッドも呼び出されました)
テーブルビューにデータを作成するメソッド:
-(void) createCategoryData
{
NSMutableArray *categoriesForJiangSu;
NSMutableArray *categoriesForZheJiang;
categorySections=[[NSMutableArray alloc]initWithObjects:@"JiangSu",@"ZheJiang", nil];
categoriesForJiangSu=[[NSMutableArray alloc]init];
categoriesForZheJiang=[[NSMutableArray alloc]init];
[categoriesForJiangSu addObject:[[NSMutableDictionary alloc]initWithObjectsAndKeys:@"package 1",@"name",
@"JiangSu.jpg",@"picture",nil]];
[categoriesForJiangSu addObject:[[NSMutableDictionary alloc]initWithObjectsAndKeys:@"package 2",@"name",
@"JiangSu.jpg",@"picture",nil]];
[categoriesForZheJiang addObject:[[NSMutableDictionary alloc]initWithObjectsAndKeys:@"package 1",@"name",
@"ZheJiang.jpg",@"picture",nil]];
[categoriesForZheJiang addObject:[[NSMutableDictionary alloc]initWithObjectsAndKeys:@"package 2",@"name",
@"ZheJiang.jpg",@"picture",nil]];
categoryData=[[NSMutableArray alloc]initWithObjects:categoriesForJiangSu,categoriesForZheJiang, nil];
[categoriesForJiangSu release];
[categoriesForZheJiang release];
}
デリゲートメソッドは次のとおりです。
-(NSInteger)numberOfSections
{
return [categorySections count];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [[categoryData objectAtIndex:section] count];
}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection: (NSInteger)section
{
return [categorySections objectAtIndex:section];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier=@"Cell";
UITableViewCell *cell=(UITableViewCell*)[tableView
dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell==nil)
{
cell=[[[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier] autorelease];
}
[[cell imageView] setImage:[UIImage imageNamed:[[[categoryData
objectAtIndex:indexPath.section] objectAtIndex:indexPath.row]
objectForKey:@"picture"]]];
[[cell textLabel] setText:[[[categoryData objectAtIndex:indexPath.section]
objectAtIndex:indexPath.row] objectForKey:@"name"]];
cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
popoverControllerのviewDidLoadメソッド:
- (void)viewDidLoad
{
[self createCategoryData];
self.contentSizeForViewInPopover=CGSizeMake(200.0, 320.0);
[super viewDidLoad];
}
そして、mainViewControllerのshowPopoverメソッド:
-(IBAction)showPopover:(id)sender
{
if(popoverController==nil)
{
popoverController=[[UIPopoverController alloc]
initWithContentViewController:popoverContentViewController];
[popoverController presentPopoverFromBarButtonItem:sender permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
popoverController.delegate=self;
}
}
popoverContentViewControllerは、popoverControllerの名前です。