现在我已经创建了一个带有 tableview 的 popoverController,并且我创建了将在 tableview 中显示的数据。但是,该表始终是空的。PS(委托和数据源已设置,委托方法也已调用)
在 tableview 中创建数据的方法:
-(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 的名称。