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

これを行うには、いくつかの異なる方法があります。1 つの方法は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 に答える