我有一个UITableView和5个uitableviewcell。每个单元格包含一个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;
}

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


当前回答

我发现使用父视图的父视图获取单元的indexPath引用的方法非常有效。感谢iphonedevbook.com (macnsmith)的提示链接文本

-(void)buttonPressed:(id)sender {
 UITableViewCell *clickedCell = (UITableViewCell *)[[sender superview] superview];
 NSIndexPath *clickedButtonPath = [self.tableView indexPathForCell:clickedCell];
...

}

其他回答

我发现使用父视图的父视图获取单元的indexPath引用的方法非常有效。感谢iphonedevbook.com (macnsmith)的提示链接文本

-(void)buttonPressed:(id)sender {
 UITableViewCell *clickedCell = (UITableViewCell *)[[sender superview] superview];
 NSIndexPath *clickedButtonPath = [self.tableView indexPathForCell:clickedCell];
...

}

这个问题分为两部分:

1)获取包含UIButton的UITableViewCell的索引路径

以下是一些建议:

Updating UIButton's tag in cellForRowAtIndexPath: method using index path's row value. This is not an good solution as it requires updating tag continuously and it does not work with table views with more than one section. Adding an NSIndexPath property to custom cell and updating it instead of UIButton's tag in cellForRowAtIndexPath: method. This solves multiple section problem but still not good as it requires updating always. Keeping a weak refence to parent UITableView in the custom cell while creating it and using indexPathForCell: method to get the index path. Seems a little bit better, no need to update anything in cellForRowAtIndexPath: method, but still requires setting a weak reference when the custom cell is created. Using cell's superView property to get a reference to parent UITableView. No need to add any properties to the custom cell, and no need to set/update anything on creation/later. But cell's superView depends on iOS implementation details. So it can not be used directly.

但是这可以使用一个简单的循环来实现,因为我们确定问题中的单元格必须在UITableView中:

UIView* view = self;
while (view && ![view isKindOfClass:UITableView.class])
    view = view.superview;
UITableView* parentTableView = (UITableView*)view;

因此,这些建议可以组合成一个简单而安全的自定义单元格方法来获取索引路径:

- (NSIndexPath *)indexPath
{
    UIView* view = self;

    while (view && ![view isKindOfClass:UITableView.class])
        view = view.superview;

    return [(UITableView*)view indexPathForCell:self];
}

从现在开始,这个方法可以用来检测哪个UIButton被按下了。

2)通知其他方按钮按下事件

在内部知道哪个UIButton在哪个自定义单元格中被按下后,这个信息需要被发送给其他方(最有可能是处理UITableView的视图控制器)。因此,这个按钮点击事件可以在类似于UITableView委托的didSelectRowAtIndexPath:方法的抽象和逻辑级别中处理。

可以使用两种方法:

a)委托:自定义单元可以有委托属性,可以定义协议。当按钮被按下时,它只是在它的委派属性上执行它的委派方法。但是在创建每个自定义单元格时,需要为它们设置这个委托属性。作为一种替代方法,自定义单元格也可以选择在父表视图的委托上执行它的委托方法。

b)通知中心:自定义单元格可以定义一个自定义的通知名称,并将该通知与userInfo对象中提供的索引路径和父表视图信息一起发布。无需为每个单元格设置任何内容,只需为自定义单元格的通知添加一个观察者就足够了。

我会像你说的那样使用tag属性,像这样设置标签:

[button setTag:indexPath.row];

然后将标签放入buttonPressedAction中,如下所示:

((UIButton *)sender).tag

Or

UIButton *button = (UIButton *)sender; 
button.tag;

Chris Schwerdt的解决方案,但后来在Swift中对我很管用:

@IBAction func rateButtonTapped(sender: UIButton) {
    let buttonPosition : CGPoint = sender.convertPoint(CGPointZero, toView: self.ratingTableView)
    let indexPath : NSIndexPath = self.ratingTableView.indexPathForRowAtPoint(buttonPosition)!

    print(sender.tag)
    print(indexPath.row)
}

我总是使用标签。

你需要子类化UITableviewCell并从那里处理按钮按下。