繁体   English   中英

如何在iOS中的UItableCell中的按钮上添加点击手势?

[英]How to add tap gesture on button inside UItableCell in iOS?

我已经创建了一个自定义单元格。在每个自定义单元格中有很多按钮,例如3到4,我想为该单元格中的每个按钮添加点击手势,以便我可以唯一地标识单击了哪个单元格的按钮。找不到任何好的解决方案。

请告诉。

您想要访问button即可直接访问,无需手势

喜欢

[yourButton1 addTarget:self action:@selector(selectButtonPressed1:) forControlEvents:UIControlEventTouchUpInside];
yourButton1.tag=indexPath.row;

 [yourButton2 addTarget:self action:@selector(selectButtonPressed2:) forControlEvents:UIControlEventTouchUpInside];
yourButton2.tag=indexPath.row;

 [yourButton3 addTarget:self action:@selector(selectButtonPressed3:) forControlEvents:UIControlEventTouchUpInside];
yourButton3.tag=indexPath.row;


 [yourButton4 addTarget:self action:@selector(selectButtonPressed4:) forControlEvents:UIControlEventTouchUpInside];
yourButton4.tag=indexPath.row;

方法-1

-(void)selectButtonPressed1:(UIButton*)sender
{
NSLog(@"selcted button1");
}

方法-2

-(void)selectButtonPressed2:(UIButton*)sender
{
NSLog(@"selcted button2");
}

方法-3

-(void)selectButtonPressed3:(UIButton*)sender
{
NSLog(@"selcted button3");
}

方法-4

-(void)selectButtonPressed4:(UIButton*)sender
{
NSLog(@"selcted button4");
}

假设您有n个按钮。 您有一种操作方法:

您需要将此方法连接到所有n个按钮(在内部向上触摸),以便每当您按下一个按钮时,此方法都会被点击。

您可以为按钮提供标签值,也可以通过标题识别它们

-(IBAction) nButtonActions:(id)sender{

         if([[sender titleLabel] isEqualtoString:@"your button-1 title"]){
                 //call your method for button -1
           }

     //or you can do the same using sender.tag


    }

使用恢复标识符。

button.restorationIdentifier = @"Cell Number _ button tag";

例如

button.restorationIdentifier = [NSString stringWithFormat:@"%@", indexPath.row];

NSString* rowIndexStr = ((UIButton*)sender).restorationIdentifier;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM