您还可以设置在tag
期间创建的每个按钮的属性tableView: cellForRowAtIndexPath:
,然后在buttonTapped
调用事件时查找sender
并找到它的tag
. tag
UIView的属性就是为这类问题提供的。
如果您需要更多信息,您可以创建一个 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]);
}