如何在单元格中使用按钮(使用StoryBoard放置)

时间:2014-02-27 22:46:24

标签: ios button cell detect

我正在寻找一种在单元格中使用按钮的方法。我已经在我的表格视图的原型单元格中创建了它,但是当我点击它时,我无法检测它所在的单元格。

你能帮帮我吗?

3 个答案:

答案 0 :(得分:1)

假设这个IBAction是点击按钮时的事件处理程序:

-   (IBAction) buttonTapped: (UIButton *) sender
    {
       //get parent cell 
       UITableViewCell *cell = [sender findSuperViewWithClass:[UITableViewCell class]];
       //do whatever you want...
    }

您需要此类别:

@implementation UIView (SuperView)

- (UIView *)findSuperViewWithClass:(Class)superViewClass
{
    UIView *superView = self.superview;
    UIView *foundSuperView = nil;

    while (nil != superView && nil == foundSuperView) {
        if ([superView isKindOfClass:superViewClass]) {
            foundSuperView = superView;
            break;
        } else {
            superView = superView.superview;
        }
    }
    return foundSuperView;
}

@end

答案 1 :(得分:0)

“我在自定义单元格中处理按钮的方式:

  • 我定义了一个IBAction,它连接到自定义单元格内的UIButton事件
  • 我为单元格定义委托和委托方法来处理按钮操作
  • IBAction调用该委托方法
  • 在cellAtRow中定义单元格时...我将tableViewController设置为委托。 cell.delegate =自我;
  • 我在tableViewController
  • 中的委托方法中做了我想做的任何动作

来自JP HribovsekHandle button click inside UITableViewCell

的回答

答案 2 :(得分:0)

- (void)buttonOnCellTapped:(id)sender
{        
    CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:self.table];
    NSIndexPath *indexPath = [self.table indexPathForRowAtPoint:buttonPosition];
    if (indexPath)
    {
        NSLog(@"BUTTON TAPPED ON SECTION: %d, ROW: %d", indexPath.section, indexPath.row);
        UITableViewCell *cell = [self.table cellForRowAtIndexPath:indexPath];
    }
}