0

我使用以下代码向我的 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);

}

4

2 回答 2

1

改变这一行的一个字符:

[playButton addTarget:self action:@selector(play) forControlEvents: UIControlEventTouchUpInside];

[playButton addTarget:self action:@selector(play:) forControlEvents: UIControlEventTouchUpInside];

当您在选择器上包含参数时,冒号实际上对于 Objective C 运行时能够在您的目标对象上查找该选择器很重要。

于 2012-07-01T19:52:31.853 回答
1

代替

[playButton viewWithTag:indexPath.row] ; 

如果您尝试接收 UIButton 的子视图(我不知道为什么),您应该使用 setter 方法设置标签:

[playButton setTag:indexPath.row];

您还必须将您的发件人转换为 UIButton 类型

-(void) play:(id) sender{
UIButton *play = (UIButton *)sender;
NSLog(@"Play %i" , play.tag);
}
于 2012-07-01T19:55:57.330 回答