2012-04-28 67 views
1

現在我創建了一個帶有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的名稱。

+0

你需要給我們一些關於你已經嘗試過的細節。我假設你在代碼中這樣做?向我們展示一些你的代碼......很難嘗試用你給的小小來幫助你。 – 2012-04-28 01:53:04

+0

它看起來像我一樣,你永遠不會將你的表添加爲子視圖。 – CodaFi 2012-04-28 05:34:06

+0

@CodaFi我是ios的初學者,所以你能告訴我如何添加表作爲子視圖嗎? – NJUHOBBY 2012-04-28 05:48:10

回答

0

作爲初學者,如果您對xib概念不清楚,請不要使用xib.Better在沒有xib的情況下完成您的工作,如CodaFi建議的那樣。將您的表視圖添加到控制器視圖中,並傳遞給popoverController。

+0

我做了你讓我做的事情,我沒有xib,我的工作沒有完成,但它也沒有工作.. – NJUHOBBY 2012-05-01 14:04:40

+0

現在真的讓我瘋了,我有不知道問題出在哪裏 – NJUHOBBY 2012-05-01 14:07:24

相關問題