检测ios中原型单元格中单击的按钮

时间:2014-11-13 08:05:20

标签: ios iphone uitableview

我创建了一个原型单元格。 Cell有一个标签和一个按钮。我已经给了两个标签。 现在我想检测从10个单元格中单击的按钮。

以前我们根据标签进行区分。但是如何用原型单元做到这一点。

我的细胞创建代码如下:

- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellIdentifier"];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cellIdentifier"];
    }

    UIButton *stopStartButton = (UIButton *)[cell viewWithTag:103];
    UILabel *chargingLabel = (UILabel *)[cell viewWithTag:102];
}


-(IBAction)stopStartButtonClicked:(id)sender
{
  NSLog(@"Button clicked");
}

2 个答案:

答案 0 :(得分:1)

您可以使用button.titleLabel.tag来区分按钮,在操作时可以与同一个标记进行比较。

第二个选项是您的按钮操作。您可以追加活动,以便为您提供有关按钮的所有信息。

例如,您只需设置

stopStartButton.titleLabel.tag=1;

-(IBAction)stopStartButtonClicked:(id)sender
{
     NSLog(@"Button clicked %d",sender.titleLabel.tag);
}

答案 1 :(得分:0)

我认为最好的方法是找到哪个单元格实际上有点按钮。您可以通过计算按钮的x原点来找到它。

- (IBAction)stopStartButtonClicked:(id)sender
{
  CGPoint pointInTable = [button convertPoint:button.bounds.origin toView:self.tableView];
  NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:pointInTable];
}

如果您有indexPath,则可以获取单元格:

UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];