How to correctly present a popover from a UITableViewCell with UIPopoverArrowDirectionRight or UIPopoverArrowDirectionLeft

后端 未结 11 825
失恋的感觉
失恋的感觉 2020-12-02 15:30

I always try to present a popover from a cell inside a tableView this way:

[myPopover presentPopoverFromRect:cell.frame inView:self.tableView permittedArrowD         


        
11条回答
  •  南笙
    南笙 (楼主)
    2020-12-02 16:12

    I had the same kind of problem, here's the workaround i used :

    • in my UITableViewCell, i added Actions (IBActions as i generate my cells from a NIB) for cell's specific buttons.
    • i then defined a CellActionDelegate protocol that mimics my actions selectors, to which i had my button (sender) and my cell (self)
    • then the detailViewController of my splitViewController implements this protocol, converting from the cell's to its coordinates...

    here a example of code

    In MyCustomTableViewCell.m :

       -(IBAction)displaySomeCellRelativePopover:(id)sender{
            //passes the actions to its delegate
            UIButton *button = (UIButton *)sender;
            [cellActionDelegate displaySomeCellRelativePopoverWithInformation:self.info
                                                                   fromButton:button 
                                                                     fromCell:self];   
       }
    

    and the, in MyDetailViewController.m :

    -(void)displaySomeCellRelativePopoverWithInformation:(MyCellInformationClass *)info
                                              fromButton:(UIButton *)button 
                                                fromCell:(UIView *)cell{
    
    UIPopoverController * popoverController = nil;
    
    //create your own UIPopoverController the way you want
    
    //Convert your button/view frame
    
    CGRect buttonFrameInDetailView = [self.view convertRect:button.frame fromView:cell];
    
    //present the popoverController
    [popoverController presentPopoverFromRect:buttonFrameInDetailView
                                   inView:self.view permittedArrowDirections:UIPopoverArrowDirectionRight animated:YES];]
    
    
    //release objects created...
    }
    

    PS : Of course, the 'action' doesn't have to be a IBAction, and the frame from where the popover originates doesn't have to be a UIButton - a single UIView would be good :)

提交回复
热议问题