2011-09-17 38 views
6

我有一個tableview,其中有兩個動作,第一個是我使用委託didSelectRowAtIndexPath,第二個我想在我的單元上的按鈕上使用動作來做其他事情。我的問題是,我沒有成功獲得indexpath?最佳做法是什麼?如何從單元格中的按鈕操作獲取tableview中的indexPath?

+0

可能DUPLIC吃了[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 *)sender; 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

如果您使用的是集合視圖,你可以使用約傑什的方法,但改變indexPathForRowAtPoint爲indexPathForItemAtPoint這樣的:

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