Ios 如何为放置在tableview的每个单元格中的UIButton编写操作?
我有一个自定义的tableview单元格,其中有一个UIButton。 我想为tableview单元格中的每个按钮编写操作。告诉我怎么做。 我尝试过使用标记,但它不能作为cellForRowAtIndexPath一次加载所有单元格,因此它会覆盖标记号。 请告诉我怎么做。 下面是我正在使用的代码Ios 如何为放置在tableview的每个单元格中的UIButton编写操作?,ios,iphone,uitableview,uibutton,Ios,Iphone,Uitableview,Uibutton,我有一个自定义的tableview单元格,其中有一个UIButton。 我想为tableview单元格中的每个按钮编写操作。告诉我怎么做。 我尝试过使用标记,但它不能作为cellForRowAtIndexPath一次加载所有单元格,因此它会覆盖标记号。 请告诉我怎么做。 下面是我正在使用的代码 if(indexPath.row == 0){[cell.viewButton addTarget:self action:@selector(playMovie) forControlEvents:UI
if(indexPath.row == 0){[cell.viewButton addTarget:self action:@selector(playMovie) forControlEvents:UIControlEventTouchUpInside];
cell.viewButton.tag = 1;
}
else {
[cell.viewButton addTarget:self action:@selector(playMovie) forControlEvents:UIControlEventTouchUpInside];
cell.viewButton.tag = 2;
}
return cell;
无需为每行添加目标,执行以下操作:
if(indexPath.row == 0){
cell.viewButton.tag = 100;
}
else {
cell.viewButton.tag = 101;
}
[cell.viewButton addTarget:self action:@selector(playMovie:) forControlEvents:UIControlEventTouchUpInside];
return cell;
-(IBAction)playMovie:(id)sender{
if(sender.tag == 100){
// Do your Stuff..
}else if(....).....
}
还使用@selector传递button的实例
[cell.viewButton addTarget:self action:@selector(playMovie:) forControlEvents:UIControlEventTouchUpInside]
cell.viewButton.tag = indexpath.row;
然后实现如下的按钮点击方法
-(void)playMovie:(UIButton *)sender{
if(sender.tag == 0){
NSLog(@"button at first cell");
}else if (sender.tag == 1){
NSLog(@"button at first cell");
}
// SO on.......
}
您可以将按钮标签赋予按钮,然后可以在其方法中标识该按钮
cell.btn.tag = indexPath.row+1;
你可以在点击事件中得到它
-(void)btnOnClick:(id)sender
{
UIButton *btnTag = (UIButton *)sender.tag;
if(btn.tag == 1)
{
}
}
您想对不同的按钮执行不同的操作还是对所有按钮执行相同的操作不同的按钮执行不同的操作,我不想使用didSelectRowAtIndexPath@jrturton它不重复。亲民党在我的问题上我已经更新了。我没有使用IBOutlet,它只是一个UIButton。它是一个复制品。无论你是否使用插座,都没有区别。您想知道单击按钮时按钮所在的单元格,对吗?为什么要添加+1?有什么特殊原因吗?在tableview中,indexPath.row以0开头,这就是为什么要添加+1,使其以+1开头。没别的了,我能理解你的所作所为。但即使不添加+1,代码也应该可以工作。。这就是为什么我问。。快乐编码:)是的,我知道然后你在点击事件中将标签与0进行比较。。。