Ios 获取UITableView上的自定义UIButton并避免点击表格行

Ios 获取UITableView上的自定义UIButton并避免点击表格行,ios,objective-c,uitableview,uibutton,Ios,Objective C,Uitableview,Uibutton,我有一个带有自定义UIButton的UITableView。 我想知道如何知道点击了哪个UIButton,然后将数据传递给void函数 我的代码是: - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *ce

我有一个带有自定义UIButton的UITableView。 我想知道如何知道点击了哪个UIButton,然后将数据传递给void函数

我的代码是:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    // Configure the cell...
    tableView.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"row320x63.png"]];

    // Cell text
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(65, 20, 220, 21)];
    label.text = [self.arrayOfAlarmSound objectAtIndex:indexPath.row];
    label.backgroundColor = [UIColor clearColor];
    label.textColor = [UIColor whiteColor];
    label.font = [UIFont fontWithName:kFONT_NAME size:kFONT_SIZE];
    label.textAlignment = NSTextAlignmentLeft;
    [cell.contentView addSubview:label];

    // Play button
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button addTarget:self action:@selector(playMusicWithIndex:) forControlEvents:UIControlEventTouchDown];
    [button setImage:[UIImage imageNamed:@"play_off"] forState:UIControlStateNormal];
    [button setImage:[UIImage imageNamed:@"play_on"] forState:UIControlStateHighlighted];
    button.frame = CGRectMake(265, 13, 35, 35);
    [cell.contentView addSubview:button];

    return cell;
}

- (void)playMusicWithIndex:(NSNumber *)alarmSoundIndex
{
    NSLog(@"Play Music");
    NSInteger fileIndex = [alarmSoundIndex integerValue];

    NSString *path;
    // Get the filename of the sound file
    switch (fileIndex)
    {
        case kALERT:
            path = [NSString stringWithFormat:@"%@%@", [[NSBundle mainBundle] resourcePath], @"/ring.wav"];
            break;
        case kMAROKAEI:
            //
            break;
        default:
            break;
    }


    // Declare a sound id
    SystemSoundID soundID;

    // get a URL for the sound file
    NSURL *filepath = [NSURL fileURLWithPath:path isDirectory:NO];

    // Use audio services to create sound
    AudioServicesCreateSystemSoundID((__bridge CFURLRef) filepath, &soundID);

    // Use audio services to play the sound
    AudioServicesPlaySystemSound(soundID);
}
如何将indexPath.Row传递给playMusicWithIndex:alarmSoundIndex? 当我使用此代码时:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    [self performSelectorOnMainThread:@selector(playMusicWithIndex:) withObject:[NSNumber numberWithInteger:indexPath.row] waitUntilDone:NO];
}
点击表格中的一行播放音频文件,但这不是我想要的,我只想在点击自定义按钮时播放音频文件


我希望我说得很清楚。

有一些不同的方法可以做到这一点。一种方法是使用按钮的标记属性:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ...

    // Play button
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.tag = indexPath.row;
    [button addTarget:self action:@selector(tap:) forControlEvents:UIControlEventTouchDown];
    [button setImage:[UIImage imageNamed:@"play_off"] forState:UIControlStateNormal];
    [button setImage:[UIImage imageNamed:@"play_on"] forState:UIControlStateHighlighted];
    button.frame = CGRectMake(265, 13, 35, 35);
    [cell.contentView addSubview:button];

    return cell;
}

- (void)tap:(UIButton *)sender
{
    [self playMusicWithIndex:[NSNumber numberWithInteger:sender.tag]];
}

有一些不同的方法可以做到这一点。一种方法是使用按钮的标记属性:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ...

    // Play button
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.tag = indexPath.row;
    [button addTarget:self action:@selector(tap:) forControlEvents:UIControlEventTouchDown];
    [button setImage:[UIImage imageNamed:@"play_off"] forState:UIControlStateNormal];
    [button setImage:[UIImage imageNamed:@"play_on"] forState:UIControlStateHighlighted];
    button.frame = CGRectMake(265, 13, 35, 35);
    [cell.contentView addSubview:button];

    return cell;
}

- (void)tap:(UIButton *)sender
{
    [self playMusicWithIndex:[NSNumber numberWithInteger:sender.tag]];
}