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

后端 未结 11 782
失恋的感觉
失恋的感觉 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:15

    This also worked for me:

    
            //Which are the ABSOLUTE coordinates in from the current selected cell
            CGRect frame =[self.view convertRect:[tbvEventsMatch rectForRowAtIndexPath:indexPath] fromView:tbvEventsMatch.viewForBaselineLayout];
    
    
    0 讨论(0)
  • 2020-12-02 16:16

    To pop a popover next to the accesory u can use this code :)

    I use this for more advanced use:

    1. finds custom accesoryView (cell.accesoryView)
    2. if empty, find generated accesoryView (UIButton) if cell has
    3. if the UIButton doesn't exists, find cell contet view (UITableViewCellContentView)
    4. if the cell contet view doesn't exists, use cell view

    Can be use for UIActionSheet or UIPopoverController.

    Here is my code:

    UIView *accessoryView       = cell.accessoryView; // finds custom accesoryView (cell.accesoryView)
    if (accessoryView == nil) {
        UIView *cellContentView = nil;
    
        for (UIView *accView in [cell subviews]) {
            if ([accView isKindOfClass:[UIButton class]]) {
                accessoryView   = accView; // find generated accesoryView (UIButton) 
                break;
            } else if ([accView isKindOfClass:NSClassFromString(@"UITableViewCellContentView")]) {
                // find generated UITableViewCellContentView                
                cellContentView = accView; 
            }
        }
        // if the UIButton doesn't exists, find cell contet view (UITableViewCellContentView)           
        if (accessoryView == nil) { 
            accessoryView   = cellContentView; 
        }
        // if the cell contet view doesn't exists, use cell view
        if (accessoryView == nil) {
            accessoryView   = cell; 
        }
    }
    
    [actionSheet showFromRect:accessoryView.bounds inView:accessoryView animated:YES];
    

    Tested in iOS 4.3 to 5.1

    Best to use as custom method:

    -(UIView*)getViewForSheetAndPopUp:(UITableViewCell*)cell;
    

    And method code:

    -(UIView*)getViewForSheetAndPopUp:(UITableViewCell*)cell {
    UIView *accessoryView = cell.accessoryView;
    
    if (accessoryView == nil) {
        UIView *cellContentView = nil;
    
        for (UIView *accView in [cell subviews]) {
            if ([accView isKindOfClass:[UIButton class]]) {
                accessoryView = accView;
                break;
            } else if ([accView isKindOfClass:NSClassFromString(@"UITableViewCellContentView")]) {              
                cellContentView = accView;
            }
        }       
    
        if (accessoryView == nil) {
            accessoryView   = cellContentView;
        }
        if (accessoryView == nil) {
            accessoryView   = cell;
        }
    }
    
    return accessoryView;
    }
    
    0 讨论(0)
  • 2020-12-02 16:18

    I came across this problem today, and I've found a simpler solution.
    When instantiating the popover, you need specify the cell's content view:

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        UIViewController *aViewController = [[UIViewController alloc] init];
        // initialize view here
    
        UIPopoverController *popoverController = [[UIPopoverController alloc] 
            initWithContentViewController:aViewController];
        popoverController.popoverContentSize = CGSizeMake(320, 416);
        UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
        [popoverController presentPopoverFromRect:cell.bounds inView:cell.contentView 
            permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
    
        [aView release];
        // release popover in 'popoverControllerDidDismissPopover:' method
    }
    
    0 讨论(0)
  • 2020-12-02 16:20

    The cell frame is going to be something like 0,0,width,size, i dont believe it will have its X and Y relative to the tableView...you want to use - (CGRect)rectForRowAtIndexPath:(NSIndexPath *)indexPath for this, this should return you the correct frame for the cell relative to the tableView...here is a link UITAbleView ref

    0 讨论(0)
  • 2020-12-02 16:23

    Here is the simple solution which works fine for me

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
        UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
        CGRect rect=CGRectMake(cell.bounds.origin.x+600, cell.bounds.origin.y+10, 50, 30);
        [popOverController presentPopoverFromRect:rect inView:cell permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
    
    }
    
    0 讨论(0)
提交回复
热议问题