ios - 检测在 UITableView 中按下了哪个 UIButton

标签 ios iphone uitableview uibutton

我有一个带有 5 个 UITableViewCellsUITableView。每个单元格都包含一个 UIButton,其设置如下:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
     NSString *identifier = @"identifier";
     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
     if (cell == nil) {
         cell = [[UITableView alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
         [cell autorelelase];

         UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(10, 5, 40, 20)];
         [button addTarget:self action:@selector(buttonPressedAction:) forControlEvents:UIControlEventTouchUpInside];
         [button setTag:1];
         [cell.contentView addSubview:button];

         [button release];
     }

     UIButton *button = (UIButton *)[cell viewWithTag:1];
     [button setTitle:@"Edit" forState:UIControlStateNormal];

     return cell;
}

我的问题是:在 buttonPressedAction: 方法中,我如何知道按下了哪个按钮。我考虑过使用标签,但我不确定这是最好的方法。我希望能够以某种方式将 indexPath 标记到控件上。

- (void)buttonPressedAction:(id)sender
{
    UIButton *button = (UIButton *)sender;
    // how do I know which button sent this message?
    // processing button press for this row requires an indexPath. 
}

执行此操作的标准方法是什么?

编辑:

我已经通过执行以下操作解决了它。我仍然想知道这是标准的做法还是有更好的方法?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
     NSString *identifier = @"identifier";
     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
     if (cell == nil) {
         cell = [[UITableView alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
         [cell autorelelase];

         UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(10, 5, 40, 20)];
         [button addTarget:self action:@selector(buttonPressedAction:) forControlEvents:UIControlEventTouchUpInside];
         [cell.contentView addSubview:button];

         [button release];
     }

     UIButton *button = (UIButton *)[cell.contentView.subviews objectAtIndex:0];
     [button setTag:indexPath.row];
     [button setTitle:@"Edit" forState:UIControlStateNormal];

     return cell;
}

- (void)buttonPressedAction:(id)sender
{
    UIButton *button = (UIButton *)sender;
    int row = button.tag;
}

需要注意的重要一点是,我无法在创建单元格时设置标记,因为单元格可能会被出队。感觉很脏。一定有更好的方法。

最佳答案

在苹果的 Accessory示例使用以下方法:

[button addTarget:self action:@selector(checkButtonTapped:) forControlEvents:UIControlEventTouchUpInside];

然后在触摸处理程序中检索触摸坐标并根据该坐标计算索引路径:

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

关于ios - 检测在 UITableView 中按下了哪个 UIButton,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1802707/

相关文章:

iphone - 如何检测 uitableViewCell 上触摸的按钮并更改该按钮的 BG 图像

ios - 为 View Controller 设置 socket /操作

ios - 标签栏图像未在 iOS 7.1 中显示

cocoa-touch - UITableViewCell 上的 textLabel.backgroundColor 不起作用

ios - UITableViewCell中图片的异步加载

iPhone - UITableViewCell 高度变化的平滑动画,包括内容更新

ios - 启动和停止 segued AVPlayer 的功能是什么?

ios - 在结构或其他方法中全局存储数组

ios - 使用蓝牙 LE 在 iOS 和 Android 之间进行通信

带有自定义单元 Storyboard 的 IOS SearchDisplayController