现在,我创建了一个带有表视图的popoverController,并且还创建了将在表视图中显示的数据。但是,该表始终为空。
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的名称。

最佳答案

作为初学者,如果您不清楚xib的概念,请不要使用xib。.最好不要使用xib来完成工作,如CodaFi建议的那样。将表视图添加到传递给popoverController的控制器视图中。

关于ios - 如何在我的popoverController中显示带有数据的tableview,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10359917/

10-10 20:29