1

我有一个带有自定义 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];
}

点击表格中的一行播放音频文件,但这不是我想要的,我只想在点击自定义 UIButton 时播放音频文件。

我希望我很清楚。

4

1 回答 1

4

有一些不同的方法可以做到这一点。一种方法是使用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]];
}
于 2013-06-08T19:51:53.717 回答