0

ユーザーが押されたときに曲をプレビューできるボタンを持つカスタムテーブルを作成しています。私のコードのほとんどは機能しますが、ボタンが押された行に対応する特定の曲をプレーヤーに渡す方法がわかりません。

たとえば、2つの行があり、#1がJay Z、#2がRed Hot Chili Peppersの場合、#1のボタンを押してJayを再生し、#2のボタンを押してPeppersを再生します。単純。コードに欠陥があり、どの行のボタンを押しても、同じ曲しか再生できません。

なぜそうなっているのかはわかりますが、解決方法がわかりません。誰かが私を正しい方向に向けることができる数本の線で私を打つことができるかどうか疑問に思っています。

didSelectRowAtIndexPath行自体が選択されたときに何か他のことが起こりたいので、使用できません。

このためのメソッドを作成する必要がありますか、それとも私が見落としているものがありますか?

ありがとう!

4

3 に答える 3

1

何かのようなもの

- (void)buttonTapped:(UIView *)sender;
{
    CGPoint pointInTableView = [sender convertPoint:sender.bounds.origin toView:self.tableView];
    NSIndexPath *tappedRow = [self.tableView indexPathForRowAtPoint:pointInTableView];

    // get song that should be played with indexPath and play it
}
于 2012-04-07T03:44:37.073 に答える
1

tableViewのようなもの:cellForRowAtIndexPath:ボタンタグをindex.rowとして指定し、以下の関数をイベント内のボタンのタッチアップにバインドします

-(void)button_click:(UIView*)sender
{
   NSInteger *index = sender.tag;
   //play song on that index
}

これはあなたを助けると思います!

于 2012-04-07T07:04:27.753 に答える
1

tag中に作成した各ボタンのプロパティを設定し、イベントが呼び出されtableView: cellForRowAtIndexPath:たときに、を検索してそのを見つけることもできます。UIViewのプロパティは、この種の問題のために提供されました。buttonTappedsendertagtag

それ以上の情報が必要な場合は、関連する曲について必要な情報の一部またはすべてを格納するUIButtonサブクラスを作成できます。もう一度、cellForRowAtIndexPathボタンがタップされたときに取得されるように、の間にその情報を設定します。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
    // Dequeue a cell and set its usual properties.
    // ...

    UIButton *playButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [playButton addTarget:self action:@selector(playSelected:) forControlEvents:UIControlEventTouchUpInside];
    // This assumes you only have one group of cells, so don't need to worry about the first index.  If you have multiple groups, you'll need more sophisticated indexing to guarantee unique tag numbers.
    [playButton setTag:[indexPath indexAtPosition:1]];

    // ...
    // Also need to set the size and other formatting on the play button, then make it the cell's accessoryView.
    // For more efficiency, don't create a new play button if you dequeued a cell containing one - just set its tag appropriately.
}

- (void) playSelected:(id) sender;
{
    NSLog(@"Play song number %d", [sender tag]);
}
于 2012-04-07T07:10:52.173 に答える