我使用以下代码向我的 tableviewcell 添加按钮,当按下按钮时,我需要知道它是哪一行。所以我已经标记了按钮(playButton viewWithTag:indexPath.row)问题是,如果我定义目标操作方法(播放)来接收发送者,它会因“无法识别的选择器”而崩溃,任何想法如何知道在哪一行按钮被按下或为什么它像这样崩溃谢谢
-(void)configureCell: (UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath {
UIButton *playButton = [UIButton buttonWithType:UIButtonTypeCustom] ;
[playButton setFrame:CGRectMake(150,5,40,40)];
[playButton viewWithTag:indexPath.row] ; //Tagging the button
[playButton setImage:[UIImage imageNamed:@"play.png"] forState:UIControlStateNormal];
[playButton addTarget:self action:@selector(play) forControlEvents: UIControlEventTouchUpInside];
[cell addSubview:playButton];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *beatCell = nil;
beatCell = [tableView dequeueReusableCellWithIdentifier:@"beatCell"];
if (beatCell == nil){
beatCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"beatCell"];
}
[self configureCell:beatCell atIndexPath:indexPath];
return beatCell;
}
-(void) play:(id) sender{
UIButton *play = sender;
NSLog(@"Play %i" , play.tag);
}