テーブルビューセルにボタンがあり、そのボタンが押されたときにそのセルをポイントするポップオーバーを表示したいとします。
まず、このようなものを使用して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を参照してください。
あなたの質問にコードを投稿し、何が起こっているのか、あなたが期待していることを説明する方が良いです。 – Anna
私はそれを貼り付けました、そのパスタのリンク:) – Anthony
didSelectRowAtIndexPathにこのコードはありますか?セルを宣言しましたが、設定していません。 – Anna