2010-11-21 4 views
0
選択されていたのtableView細胞からの私のポップオーバーを表示しようとすると

UIPopover問題

UITableViewCell *cell; 
UserProfile *switchV = [[UserProfile alloc] initWithNibName:nil bundle:nil]; 
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:switchV]; 
    UIPopoverController *pop = [[UIPopoverController alloc] initWithContentViewController:navController]; 
[pop presentPopoverFromRect:cell.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES]; 

[switchVリリース];

私は間違っていますか?

+0

あなたの質問にコードを投稿し、何が起こっているのか、あなたが期待していることを説明する方が良いです。 – Anna

+0

私はそれを貼り付けました、そのパスタのリンク:) – Anthony

+0

didSelectRowAtIndexPathにこのコードはありますか?セルを宣言しましたが、設定していません。 – Anna

答えて

0

テーブルビューセルにボタンがあり、そのボタンが押されたときにそのセルをポイントするポップオーバーを表示したいとします。

まず、このようなものを使用してcellForRowAtIndexPathでセルにボタンを追加:上記
(RECTを四捨五入する必要はありません)

UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
[button setFrame:CGRectMake(100, 0, 100, 30)]; 
[button setTitle:@"Button" forState:UIControlStateNormal]; 
[button addTarget:self action:@selector(profileUser:) 
         forControlEvents:UIControlEventTouchUpInside]; 
[cell.contentView addSubview:button]; 

重要な点は、@selectorでということですprofileUserの後にコロンがあります(これは、profileUserの最初のパラメータとして自分自身への参照を送信するようにボタンに指示します)。この参照は、どのセルが選択されたかを知るために使用できます。

profileUser:

-(void)profileUser:(UIButton *)button 
{ 
    UITableViewCell *cell = (UITableViewCell *)[[button superview] superview]; 
    //first superview is cell.contentView 
    //second superview is cell 

    UserProfile *switchV = [[UserProfile alloc] initWithNibName:nil bundle:nil]; 
    UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:switchV]; 
    UIPopoverController *pop = [[UIPopoverController alloc] initWithContentViewController:navController]; 

    [pop presentPopoverFromRect:cell.frame inView:self.view 
     permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES]; 

    [switchV release]; 
    [navController release]; 

    self.popoverController = pop; //save in property for later release 

    [pop release]; 
} 

可能であれば、UIPopoverArrowDirectionAnyとして矢印方向を残して、それはそれを置くのに最適な場所を把握しましょう:メソッドは次のようなものでなければなりません。

は編集:、画面上のセルの位置に応じて、しかし

[pop presentPopoverFromRect:button.frame inView:cell 
    permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES]; 


ではなく、セルのボタンに上向きの矢印とポップオーバーを表示するには、これを使用しますポップオーバーがボタンのすぐ下に表示されないことがあります。結果が分からない限り、「上」の代わりに「すべて」を使用してください。

また、後でリリースする(deallocで)ために、popoverコントローラへの参照を保存してください。そうしないと、メソッドの[pop release]がクラッシュする可能性があります。詳細な例については、sample app Popoversを参照してください。

+0

ねえ、ありがとう!では、ボタンフレームを使ってどのように表示するのですか?ボタンがセルのどこに表示されているのかを右下に示します。私はそのフレームを使ってみましたが、最新のセルだけを表示し、他のセルは表示しません。 – Anthony

+0

答えを更新しました。最新のセルで表示され、他のセルで表示されていないことを意味するかどうかはわかりません。 – Anna

関連する問題