2011-09-17 8 views
6

私はそれに2つのアクションを持つテーブルビューを持っています。私はデリゲートのdidSelectRowAtIndexPathを使用しています。もう1つは、セルのボタンで何か他の操作をしたいのです。私の問題は、私がindexpathを取得するのに成功しないということです?それを行うベストプラクティスは何ですか?セル内のボタンアクションからテーブルビュー内のindexPathを取得する方法は?

+0

可能な複製ate of [IOS 7 - UITableViewCellに配置されたボタンからindexPathを取得する方法](http://stackoverflow.com/questions/19000356/ios-7-how-to-get-the-indexpath-from-button-placed- in-uitableviewcell) –

答えて

9

あなたは

[cell.contentView addSubview:button]; 

、としてセルにボタンを追加した場合、その後、あなたはインデックスパスをとして取得することができ、ここで

- (void)onButtonTapped:(UIButton *)button { 

    UITableViewCell *cell = (UITableViewCell *)button.superview.superview; 
    NSIndexPath *indexPath = [tableView indexPathForCell:cell]; 
    // Go ahead 
} 
+0

ありがとう! UIButton * button =(UIButton *)送信者。 UITableViewCell * cell =(UITableViewCell *)button.superview.superview; –

+0

よろしく!ようこそ! – EmptyStack

+1

スーパービューからtableViewにアクセスすべきではありません。これは動作しますが、新しいiOSでは動作しない可能性があります。 6/7 Appleからの古典的な例は別のスーパービューを追加しました。これはクラッシュするでしょう – CW0007007

1

は、カスタムボタンの完全なコードです:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 

    CustomCellFilter *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (!cell) {cell = [[CustomCellFilter alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];} 

    //configure your cell here 
    cell.titleLabel.text = @"some title"; 

    //add button 
    UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
    resetButton.frame = CGRectMake(40, 10, 18, 18); 
    [resetButton addTarget:self action:@selector(onButtonTapped:) forControlEvents:UIControlEventTouchUpInside]; 
    [resetButton setImage:[UIImage imageNamed:@"someimage.png"] forState:UIControlStateNormal]; 
    [cell.contentView addSubview:myButton]; 

    //afterwards return the cell 
    return cell; 

- (void)onButtonTapped:(UIButton *)button { 

    UITableViewCell *cell = (UITableViewCell *)button.superview.superview; 
    NSIndexPath *indexPath = [filterTable indexPathForCell:cell]; 
    NSLog(@"%@", indexPath); 
    //use indexPath here... 
} 
-1

ボタンが移動された場合、またはAppleがアクセサリビューのビュー階層を変更した場合、このメソッドはチェーンを上にして表示されますUITableViewCellのために:

- (void)tapAccessoryButton:(UIButton *)sender { 
    UIView *parentView = sender.superview; 

// the loop should take care of any changes in the view heirarchy, whether from 
// changes we make or apple makes. 
    while (![parentView.class isSubclassOfClass:UITableViewCell.class]) 
     parentView = parentView.superview; 

    if ([parentView.class isSubclassOfClass:UITableViewCell.class]) { 
     UITableViewCell *cell = (UITableViewCell *) parentView; 
     NSIndexPath *indexPath = [self.tableView indexPathForCell:cell]; 
     [self tableView:self.tableView accessoryButtonTappedForRowWithIndexPath:indexPath]; 
    } 
} 
2

私は、このソリューションを使用しています - あなたはコレクションビューを使用している場合はポイント

CGPoint center= [sender center]; 
CGPoint rootViewPoint = [[sender superview] convertPoint:center toView:_tableView1]; 
NSIndexPath *indexPath = [_tableView1 indexPathForRowAtPoint:rootViewPoint]; 
NSLog(@"%@",indexPath); 

その作業完璧

+0

これは 'superView.superView'を使う代わりにもっと適切です – Mrug

0

を使用してセルのインデックスパスを取得するには、 Yogeshメソッドを使用できますが、indexPathForItemAtPointのindexPathForRowAtPointを次のように変更してください:

CGPoint center= [sender center]; 
CGPoint rootViewPoint = [[sender superview] convertPoint:center toView:_tableView1]; 
NSIndexPath *indexPath = [_tableView1 indexPathForRowAtPoint:rootViewPoint]; 
NSLog(@"%@",indexPath); 
関連する問題