UIPopoverArrowDirectionRight または UIPopoverArrowDirectionLeft を使用して UITableViewCell からポップオーバーを正しく表示する方法

StackOverflow https://stackoverflow.com/questions/3015400

質問

私は常に次の方法で tableView 内のセルからポップオーバーを表示しようとしています。

[myPopover presentPopoverFromRect:cell.frame inView:self.tableView permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];

しかし、iPopoverArrowdirectionrightまたは左を使用することはできません。なぜなら、iPad(ポートレートまたは風景)の位置に応じて、ポップオーバーが他の場所に表示されるからです。

ポップオーバーを正しい方法で表示していますか?

追伸:テーブル ビューは、splitView のdetailView 内にあります。

役に立ちましたか?

解決

ここでは私のために罰金を作品の簡単な解決策がある。

- (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];

}

他のヒント

あなたはrectForRowAtIndexPathメソッドを介して細胞からフレームを取得しています。これは正しいです。しかし、テーブルビューには、最も可能性の高いポップオーバーは、座標を取得するときに、より大きなiPadのビューのサブビューは、それは彼らが拡大していると考えています。ポップオーバーが間違った場所に表示されるのはこのためです。

実施例、行のCGRectは(0,40,320,44)です。代わりに、テーブルビューに、そのフレームをターゲットにポップオーバーのではなく、あなたのメインビューにそのフレームを対象としています。

私は私の拡大における座標テーブルの相対座標からフレームを変換することによって、この問題を解決します。

コード:

CGRect aFrame = [self.myDetailViewController.tableView rectForRowAtIndexPath:[NSIndexPath indexPathForRow:theRow inSection:1]];
[popoverController presentPopoverFromRect:[self.myDetailViewController.tableView convertRect:aFrame toView:self.view] inView:self.view permittedArrowDirections:UIPopoverArrowDirectionRight animated:YES];
この問題を探して他の人を助けます。

希望。

私は今日、この問題に出くわした、と私は簡単な解決策を見つけた。
ポップオーバーをインスタンス化する場合、あなたはセルのコンテンツビューを指定する必要があります。

- (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
}

でスウィフト、上記の回答の間の任意の方向で計算された上で、私にとって、この作品ます:

if let popOverPresentationController : UIPopoverPresentationController = myAlertController.popoverPresentationController {

    let cellRect = tableView.rectForRowAtIndexPath(indexPath)

    popOverPresentationController.sourceView                = tableView
    popOverPresentationController.sourceRect                = cellRect
    popOverPresentationController.permittedArrowDirections  = UIPopoverArrowDirection.Any

}

ポップでpopoverの付属u利用できるコード:)

その結果が、先ほど任天堂さんのために。 高度な利用の

  1. がカスタムaccesoryView(細胞です。accesoryView)
  2. (空の場合、生成されaccesoryView(UIButton)が細胞は
  3. 場合にはUIButtonなが存在し、細胞contetビュー(UITableViewCellContentView)
  4. 場合にはcontet眺めなが存在し、セルビュー

で用いられることと UIActionSheet または UIPopoverController.

こちらは自分のコード:

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];

試験iOS4.3 5.1

使用してカスタムメイドの方法

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

法コード:

-(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;
}
私もこの問題に遭遇しました。私のためのソリューションは、単にCGRect)rectForRowAtIndexPath:(NSIndexPath *)indexPathによって返された矩形の幅を変更することでした。

CGRect rect = [aTableView rectForRowAtIndexPath:indexPath];

//create a 10 pixel width rect at the center of the cell

rect.origin.x = (rect.size.width - 10.0) / 2.0; 
rect.size.width = 10.0;  

[self.addExpensePopoverController presentPopoverFromRect:rect inView:aTableView permittedArrowDirections:UIPopoverArrowDirectionAny  animated:YES]; 

これは、細胞内の中央に矩形を作成します。この方法では、ポップオーバーが位置自体にfindind良いスポットのより多くのチャンスを持っています。

セルフレームは0,0,width,sizeのようなものになります。tableViewに相対的なXとYを持つとは思いません...使用したいのは - (CGRect)rectForRowAtIndexPath:(NSIndexPath *)indexPathこれは、tableView に関連するセルの正しいフレームを返すはずです...ここにリンクがあります UITableView 参照

私は同じような問題は、こちらのプを使った:

  • 私のUITableViewCellしているものであり、発表という行為(IBActionsどを私の細胞からのペン先)を細胞の特定のボタンを押します。
  • そして定義CellActionDelegateプロトコルを模した自動セレクタであったことから私のボタン(送信者)と私の細胞(self)
  • その後、detailViewControllerのsplitViewControllerを実装するプロトコル変換からの細胞にその座標を...

この例のコード

にMyCustomTableViewCell.m:

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

に、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:もちろん、行動する必要はありません、IBAction、フレームからのpopoverに起因する必要はありません、UIButton-シUIViewい:)

これは私がやった方法ですと罰金完璧に動作します。

RidersVC *vc = [RidersVC ridersVC];
vc.modalPresentationStyle = UIModalPresentationPopover;
vc.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
UIPopoverPresentationController *popPresenter = [vc popoverPresentationController];
popPresenter.sourceView = vc.view;
popPresenter.barButtonItem= [[UIBarButtonItem alloc] initWithCustomView:button];
popPresenter.backgroundColor = [UIColor colorWithRed:220.0f/255.0f green:227.0f/255.0f blue:237.0f/255.0f alpha:1.0];
[self.parentVC presentViewController:vc animated:YES completion:NULL];

これも私のために働います:

        //Which are the ABSOLUTE coordinates in from the current selected cell
        CGRect frame =[self.view convertRect:[tbvEventsMatch rectForRowAtIndexPath:indexPath] fromView:tbvEventsMatch.viewForBaselineLayout];

受け入れ答えは、今コンパイルすることはできません。

CGRect rect =[tableView rectForRowAtIndexPath:indexPath]; //This is how to get the correct position on the tableView    
UIPopoverPresentationController *popController = [controller popoverPresentationController];
popController.sourceRect = rect;
popController.permittedArrowDirections = 0;  //Here there is no arrow. It is my app's need
popController.delegate = self;
popController.sourceView = self.tableView;
popController.sourceView.backgroundColor = [UIColor yellowColor];
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top