tag
中に作成した各ボタンのプロパティを設定し、イベントが呼び出されtableView: cellForRowAtIndexPath:
たときに、を検索してそのを見つけることもできます。UIViewのプロパティは、この種の問題のために提供されました。buttonTapped
sender
tag
tag
それ以上の情報が必要な場合は、関連する曲について必要な情報の一部またはすべてを格納する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]);
}