1

これで、テーブルビューを含む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の名前です。

4

1 に答える 1

0

初心者として、xibの概念が明確でない場合は、xibを使用しないでください。CodaFiが提案したように、xibなしで作業を行う方がよいでしょう。テーブルビューをコントローラービューに追加して、popoverControllerに渡します。

于 2012-04-28T06:17:14.363 に答える