現在我創建了一個帶有tableview的popoverController,並且還創建了將在tableview中顯示的數據。但是,表格總是空的。 PS(委託和數據源已定,委託方法被調用,太)如何使用數據在我的popoverController中顯示我的tableview
,在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 *[email protected]"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;
}
viewDidLoad方法爲popoverController:
- (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的名稱。
你需要給我們一些關於你已經嘗試過的細節。我假設你在代碼中這樣做?向我們展示一些你的代碼......很難嘗試用你給的小小來幫助你。 – 2012-04-28 01:53:04
它看起來像我一樣,你永遠不會將你的表添加爲子視圖。 – CodaFi 2012-04-28 05:34:06
@CodaFi我是ios的初學者,所以你能告訴我如何添加表作爲子視圖嗎? – NJUHOBBY 2012-04-28 05:48:10