Reputation: 41
how can we implement a table view in a popover in ipad. i have to give a button in my toolbar on clicking which a popover should display with a table view in it with 4 rows.Can anyone please help me with this.
Thanks in adavnce
Upvotes: 0
Views: 7770
Reputation: 131
You can use the below given code.
First you have to create a tableView.For that use the below given code.
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [recipe.ingredients count];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [recipe.ingredients count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *TableIdentifier=@"Cell";
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:TableIdentifier];
if (cell==nil) {
cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:TableIdentifier];
}
cell.textLabel.text=[recipe.ingredients objectAtIndex:indexPath.row];
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
cell.textLabel.textColor=[UIColor blackColor];
cell.textLabel.font=[UIFont fontWithName:@"Helvetica" size:18];
return cell;
}
Then add the method for button click
-(IBAction)setData:(id)sender
{
UIViewController *popoverContent=[[UIViewController alloc] init];
UITableView *tableView=[[UITableView alloc] initWithFrame:CGRectMake(265, 680, 0, 0) style:UITableViewStylePlain];
UIView *popoverView=[[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 300)];
popoverView.backgroundColor=[UIColor whiteColor];
popoverContent.view=popoverView;
popoverContent.contentSizeForViewInPopover=CGSizeMake(200, 420);
popoverContent.view=tableView; //Adding tableView to popover
tableView.delegate=self;
tableView.dataSource=self;
self.popoverController=[[UIPopoverController alloc] initWithContentViewController:popoverContent];
[self.popoverController presentPopoverFromRect:CGRectMake(400, 675, 0, 0) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionDown animated:YES];
}
Upvotes: 4
Reputation: 19879
You just need to add the tableview as the view of the popover viewController.
PopViewController *popViewController = [[popViewController alloc]
initWithNibName:@"PopViewController"
bundle:[NSBundle mainBundle]];
UITableView *tableView = [[UITableView aloc]initWithStyle:UITableViewStyle...];
//Remember to set the table view delegate and data provider
popViewController.view = tableView;//OR [popViewController.view addSubView:tableView]
UIPopoverController *popover = [[UIPopoverController alloc] initWithContentViewController:popViewController];
An other Way will be to set the popover viewController as a TableViewController.
Hope it helps
Upvotes: -1
Reputation: 1075
I have ChooseAdsViewController class for the table. To show it I use:
ChooseAdsViewController* adsController = [[ChooseAdsViewController alloc] initWithStyle:UITableViewStyleGrouped];
adsController.images = m_images;
UINavigationController* navController = [[UINavigationController alloc] initWithRootViewController:adsController];
UIPopoverController *popover = [[UIPopoverController alloc] initWithContentViewController:navController];
[popover setPopoverContentSize:CGSizeMake(320, 560)];
self.popoverController = popover;
popoverController.delegate = self;
[popoverController presentPopoverFromRect:sender.frame
inView:self.scrollView
permittedArrowDirections:UIPopoverArrowDirectionAny
animated:YES];
[popover release];
[navController release];
[adsController release];
Navigation controller not needed, in general.
Upvotes: 1