Search code examples
iosipaduipopovercontroller

Display UIPopover just under selected section


I have a UIButton in each section, i want to display a UIPopover each time when user taps on the UIButton. Im able to display the popover, but the problem is, the popover is displayed only on first section, even if i tap on some other section in the UITableView. Below is the code i have written to display the popover.

UIViewController* popoverContent = [[UIViewController alloc] init]; //ViewController

UIView *popoverView = [[UIView alloc] init];   //view
popoverView.backgroundColor = [UIColor clearColor];

UIDatePicker *datePicker=[[UIDatePicker alloc]init];//Date picker
datePicker.frame=CGRectMake(0,44,320, 216);
datePicker.datePickerMode = UIDatePickerModeDateAndTime;
[datePicker setMinuteInterval:5];
[datePicker setTag:10];
[datePicker addTarget:self action:@selector(result) forControlEvents:UIControlEventValueChanged];
[popoverView addSubview:datePicker];

popoverContent.view = popoverView;
popoverController = [[UIPopoverController alloc] initWithContentViewController:popoverContent];
popoverController.delegate=self;
[popoverController setPopoverContentSize:CGSizeMake(320, 264) animated:NO];
[popoverController presentPopoverFromRect:headerBtn.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];

Thanks in advance.


Solution

  • Its expected, you need to maintain the reference of headerbtn as unique by using tag property for every section ;

    Try something similar :

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
      // ....
      headerbtn.tag = indexPath.section+25;
    
    }
    

    Action for headerbtn

    NSIndexPath *selectedIP = [NSIndexPath indexPathForRow:0 inSection:btn.tag - 25];
    UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:selectedIP];
    
    // Use the cell reference to point out the popOver 
    
    // ...........
    
     [popoverController presentPopoverFromRect:[cell convertRect:btn.frame toView:self.view] inView:self.view permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];