2011-09-13 10 views
9

私はこれを解決する方法を探していましたが、私にはうまくいかないものを見つけることができませんでした。私は内部にボタン付きのカスタムセルを持っています。私の問題は、どのように私は、アクションメソッドにindexPathを渡すのですか?カスタムUITableViewCellボタンアクション?

今、私は私のcellForRowAtIndexPathメソッドで

[cell.showRewards addTarget:self action:@selector(myAction:) forControlEvents:UIControlEventTouchUpInside]; 

をやっているし、私の方法は次のとおりです。

-(IBAction)myAction:(id)sender{ 
NSIndexPath *indexPath = [self.tableView indexPathForCell:(MyCustomCell *)[sender superview]]; 
NSLog(@"Selected row is: %d",indexPath.row); 
} 

任意のヒント?ありがとう。

+0

あなたはdidSelectRowを使用している:メソッドを? – tipycalFlow

答えて

5

やりたいことであれば関数の中でそれをチェック行く1つの方法です。セルが再利用されるたびに、適切なタグがボタンオブジェクト上で更新されるようにコードを記述する必要があります。

代わりに、私はこれがうまくいくと感じています。これを試してみてください -

-(IBAction)myAction:(id)sender 
{ 
CGPoint location   = [sender locationInView:self.tableView]; 
NSIndexPath *indexPath  = [self.tableView indexPathForRowAtPoint:location]; 
UITableViewCell *swipeCell = [self.tableView cellForRowAtIndexPath:indexPath]; 

NSLog(@"Selected row: %d", indexPath.row); 
//...... 
} 

を基本的に何をクリックして、tableViewに関して起こった場所の座標を取得して行っています。座標を取得した後、tableViewは、indexPathForRowAtPoint:メソッドを使用してindexPathを与えることができます。あなたはこの後に行くのが良いです...

Voila、あなたはちょうどindexPathだけでなく、クリックが起こった実際のセルを持っています。 (そのNSArrayのを仮定して)自分のデータソースから実際のデータを取得するには、行うことができます - カスタムUITableViewCellクラスで

[datasource objectAtIndex:[indexPath row]];

+0

私は約NSIndexPathを提案しようとしていた* indexPath = [tableView indexPathForRowAtPoint:[[[イベントtouchesForView:ボタン] anyObject] locationInView:tableView]];しかし、これははるかにきれいです、ありがとう! – phi

+3

うーん、ここに何か不足していますか?実際には[送信側locationInView:self.tableView]です。あなたのために働く? – phi

+0

私のためではありません。私は[UIButton locationInView:]を取得します:認識できないセレクタがインスタンスに送信されました。 – djskinner

2

あなたは、このいずれかを試してみてください

cell.showRewards.tag = indexPath.row; 

-(IBAction)myAction:(id)sender 
{ 
    NSIndexPath *indexPath = [self.tableView indexPathForCell:[sender tag]]; 
    NSLog(@"Selected row is: %d",indexPath.row); 
} 
3

のようにあなたの方法でボタンタグとアクセスにindexpathを割り当てることができます。

cell.showRewards.tag=indextPath.row 

これをcellforrowatindexpath tableviewのメソッドで実装します。

-(IBAction)myAction:(id)sender{ 
UIButton* btn=(UIButton*)sender; 
NSLog(@"Selected row is: %d",btn.tag); 
} 
3

あなたは= indexpathをbuttonタグ値を設定し、タグ値が、私はボタンのtagを設定感じるが、これはuが

7
cell.showRewards.tag = indexPath.row; 

-(IBAction)myAction:(id)sender 
{ 
UIButton *btn = (UIButton *)sender; 
int indexrow = btn.tag; 
NSLog(@"Selected row is: %d",indexrow); 
} 
+3

これは、UIButtonの代わりに使用するlocationInView – JulianB

2

:あなたが書くことができますcellForRowAtIndexPath方法で

[self.contentView addSubview:but_you]; 

[cell.showRewards addTarget:self action:@selector(myAction:) forControlEvents:UIControlEventTouchUpInside];  
cell.showRewards.tag = indexPath.row; 
1

[sender superview]にアクセスすると、MyCustomCellにはアクセスしませんが、それはcontentViewです。 Read UITableViewCellクラスリファレンス:

contentView セルオブジェクトのコンテンツビューを返します。(読み取り専用)

@property(nonatomic, readonly, retain) UIView *contentView 

考察: UITableViewCellオブジェクトのコンテンツビューは、セルによって表示されるコンテンツのデフォルトのスーパーです。追加のビューを追加するだけでセルをカスタマイズする場合は、ビューをコンテンツビューに追加して、セルが編集モードに出入りする際に適切に配置されるようにする必要があります。

コードを変更する最も簡単な方法は、[[sender superview] superview]を使用することです。 後でセルを変更して別のビューにボタンを挿入すると、これは機能しなくなります。 contentViewがiPhoneOS 2.0に掲載されました。同様の将来の変更があなたのコードに影響します。私がこのように使うことを提案しない理由。

1

私は本当にこれにまともな解決策がないことが信じられないことがわかります。

何らかの理由で、私はタグ付け方法を見つけ、他の答えに少し汚れている正しいモデルオブジェクトを特定するために画面上のセルの視覚的位置を使用します。

サブクラスUITableViewCellの

私はcellForRowAtIndexPath:にサブクラスにUITableViewCellの

@interface MyCustomCell : UITableViewCell 

@property (nonatomic, strong) Model *myModelObject; 

@end 

セルを作成したといったソリューションあなたは以下のとおりです。

は、ここで問題には2つの異なるアプローチですモデルオブジェクトを使用してセルデータを取り込む可能性があります。このメソッドでは、モデルオブジェクトをセルに割り当てることができます。

し、ボタンタップハンドラで:

MatchTile *cell = (MatchTile *) sender.superview.superview; 

if (cell && cell.myModelObject) 
{ 
    //Use cell.myModelObject 
} 

私は正直に言うと、この溶液を用いて、100%満足していません。このような特殊なUIKitコンポーネントにドメインオブジェクトをアタッチするのは悪い習慣のようです。あなたはセルとモデルオブジェクトを関連付けると、後でそれを取得するために使用することができます詐欺の別のビットがあるセルをサブクラス化したくない場合は

使用のObjective-Cの連想は

オブジェクト。

モデルオブジェクトをセルから取得するには、セルを識別するために一意のキーが必要です。このようなものを定義します。あなたは、細胞を移入するモデルオブジェクトを使用しているとき

static char* OBJECT_KEY = "uniqueRetrievalKey"; 

は、あなたのcellForRowAtIndexPath:メソッドに次の行を追加します。これにより、モデルオブジェクトとセルオブジェクトが関連付けられます。私はすでにセルをサブクラス化したいので、私は(最初を選んだものの、反射で

MyModelObject *myModelObject = (MyModelObject *) objc_getAssociatedObject(cell, OBJECT_KEY); 

:その後、

objc_setAssociatedObject(cell, OBJECT_KEY, myModelObject, OBJC_ASSOCIATION_RETAIN); 

とは、どこでも、あなたが使用してモデルオブジェクトを取得することができ、そのセルへの参照を持っています)、2番目の解決策はおそらく、モデルオブジェクトをアタッチして取得するためにViewControllerの責任を負うので、おそらく少し洗っています。UITableViewCellはそれについて何も知る必要はありません。

8

私はすべての最良の解決策だと思うものを追加したいだけです:UIViewのカテゴリ。

それは、このような単純なのです:

- (void)somethingHappened:(id)sender 
{ 
    NSIndexPath *indexPath = [self.tableView indexPathForCell:[sender parentCell]]; 

    // Your code here... 
} 

はただのUIViewにこのカテゴリを使用します。- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath方法で

@interface UIView (ParentCell) 

- (UITableViewCell *)parentCell; 

@end 

@implementation UIView (ParentCell) 

- (UITableViewCell *)parentCell 
{ 
    UIView *superview = self.superview; 
    while(superview != nil) { 
     if([superview isKindOfClass:[UITableViewCell class]]) 
      return (UITableViewCell *)superview; 

     superview = superview.superview; 
    } 

    return nil; 
} 

@end 
+1

これは実際には素晴らしいカテゴリで、iOS 7と manderson

1

は、以下のコードを記述します。

[cell.zoomButton addTarget:self action:@selector(navigateAction:) forControlEvents:UIControlEventTouchUpInside]; 
cell.zoomButton.tag=indexPath.row; 

次にメソッドを書きますこのように:

-(IBAction)navigateAction:(id)sender 
{ 
UIButton *btn = (UIButton *)sender; 
int indexrow = btn.tag; 
NSLog(@"Selected row is: %d",indexrow); 

currentBook = [[bookListParser bookListArray] objectAtIndex:indexrow]; 

KitapDetayViewController *kitapDetayViewController; 
if(IS_IPHONE_5) 
{ 
    kitapDetayViewController = [[KitapDetayViewController alloc] initWithNibName:@"KitapDetayViewController" bundle:Nil]; 
} 

else 
{ 
    kitapDetayViewController = [[KitapDetayViewController alloc] initWithNibName:@"KitapDetayViewController_iPhone4" bundle:Nil]; 
} 
kitapDetayViewController.detailImageUrl = currentBook.detailImageUrl; 
kitapDetayViewController.bookAuthor = currentBook.bookAuthor; 
kitapDetayViewController.bookName = currentBook.bookName; 
kitapDetayViewController.bookDescription = currentBook.bookDescription; 
kitapDetayViewController.bookNarrator=currentBook.bookNarrator; 
kitapDetayViewController.bookOrderHistory=currentBook.bookOrderDate; 
int byte=[currentBook.bookSizeAtByte intValue]; 
int mb=byte/(1024*1024); 
NSString *mbString = [NSString stringWithFormat:@"%d", mb]; 
kitapDetayViewController.bookSize=mbString; 
kitapDetayViewController.bookOrderPrice=currentBook.priceAsText; 
kitapDetayViewController.bookDuration=currentBook.bookDuration; 
kitapDetayViewController.chapterNameListArray=self.chapterNameListArray; 
// [[bookListParser bookListArray ]release]; 
[self.navigationController pushViewController:kitapDetayViewController animated:YES]; 
} 
1

ボタンのインデックスパスを希望する場合は、Detecting which UIButton was pressed in a UITableViewで説明してください。

基本的にボタンアクションは次のようになります。ここでは

- (void)checkButtonTapped:(id)sender 
    { 
     CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:self.tableView]; 
     NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:buttonPosition]; 
     if (indexPath != nil) 
     { 
     ... 
     } 
    } 
0

は(IOS11上でテスト済み)それを行うには、 "最も簡単な方法" である:

NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint 
[[[sender superview] superview] center]]; 
関連する問題