Ios 如何检测准确选择了哪个单元格的按钮

Ios 如何检测准确选择了哪个单元格的按钮,ios,xcode,uitableview,interface-builder,Ios,Xcode,Uitableview,Interface Builder,假设我有一个自定义的UITableViewCell包含UIButton控件。当用户按下按钮时,它应根据选择的单元格向另一个视图控制器显示适当的信息(更准确地说,我有一个对象数组用于表示UITableView中的信息,我想将此信息传输到下一个视图控制器)。问题是,我如何才能检测到准确选择了哪个单元格上的按钮 提前感谢。试试这个: 在cellForRowAtIndexpath中插入以下代码 [cell.yourbuttonName addTarget:self action:@selector(yo

假设我有一个自定义的
UITableViewCell
包含
UIButton
控件。当用户按下按钮时,它应根据选择的单元格向另一个视图控制器显示适当的信息(更准确地说,我有一个对象数组用于表示
UITableView
中的信息,我想将此信息传输到下一个视图控制器)。问题是,我如何才能检测到准确选择了哪个单元格上的按钮

提前感谢。

试试这个:

在cellForRowAtIndexpath中插入以下代码

[cell.yourbuttonName addTarget:self action:@selector(yourbuttonNamePressed:) forControlEvents:UIControlEventTouchDown];
将下面的代码粘贴为单独的方法

- (void)yourbuttonNamePressed:(UIButton *)sender
{
CGPoint swipepoint = sender.center;
CGPoint rootViewPoint = [sender.superview convertPoint:swipepoint toView:self.goalsTable];
NSIndexPath *indexPath = [self.goalsTable indexPathForRowAtPoint:rootViewPoint];
}
试试这个:

在cellForRowAtIndexpath中插入以下代码

[cell.yourbuttonName addTarget:self action:@selector(yourbuttonNamePressed:) forControlEvents:UIControlEventTouchDown];
将下面的代码粘贴为单独的方法

- (void)yourbuttonNamePressed:(UIButton *)sender
{
CGPoint swipepoint = sender.center;
CGPoint rootViewPoint = [sender.superview convertPoint:swipepoint toView:self.goalsTable];
NSIndexPath *indexPath = [self.goalsTable indexPathForRowAtPoint:rootViewPoint];
}
试试这个:

在cellForRowAtIndexpath中插入以下代码

[cell.yourbuttonName addTarget:self action:@selector(yourbuttonNamePressed:) forControlEvents:UIControlEventTouchDown];
将下面的代码粘贴为单独的方法

- (void)yourbuttonNamePressed:(UIButton *)sender
{
CGPoint swipepoint = sender.center;
CGPoint rootViewPoint = [sender.superview convertPoint:swipepoint toView:self.goalsTable];
NSIndexPath *indexPath = [self.goalsTable indexPathForRowAtPoint:rootViewPoint];
}
试试这个:

在cellForRowAtIndexpath中插入以下代码

[cell.yourbuttonName addTarget:self action:@selector(yourbuttonNamePressed:) forControlEvents:UIControlEventTouchDown];
将下面的代码粘贴为单独的方法

- (void)yourbuttonNamePressed:(UIButton *)sender
{
CGPoint swipepoint = sender.center;
CGPoint rootViewPoint = [sender.superview convertPoint:swipepoint toView:self.goalsTable];
NSIndexPath *indexPath = [self.goalsTable indexPathForRowAtPoint:rootViewPoint];
}

简单的解决方案是,当您在CellForRowAtIndexPath中配置单元格时,您需要设置cell.button.tag=100+indexPath.row

-(void)btnClicked:(UIButton *)sender
{
NSIndexPath* path= [NSIndexPath indexPathForRow:sender.tag-100 inSection:0];
UITableViewCell*cell=(UITableViewCell *)[tblView cellForRowAtIndexPath:path];
}
注意:您需要从UITableviewcell中删除addTarget和buttonClicked实现
在cellForRowAtIndexPath中,方法addtarget to button,在这个类中,方法implemnt buttonClicked.*

简单的解决方案是,当您在cellForRowAtIndexPath中配置单元格时,需要设置cell.button.tag=100+indexPath.row

-(void)btnClicked:(UIButton *)sender
{
NSIndexPath* path= [NSIndexPath indexPathForRow:sender.tag-100 inSection:0];
UITableViewCell*cell=(UITableViewCell *)[tblView cellForRowAtIndexPath:path];
}
注意:您需要从UITableviewcell中删除addTarget和buttonClicked实现
在cellForRowAtIndexPath中,方法addtarget to button,在这个类中,方法implemnt buttonClicked.*

简单的解决方案是,当您在cellForRowAtIndexPath中配置单元格时,需要设置cell.button.tag=100+indexPath.row

-(void)btnClicked:(UIButton *)sender
{
NSIndexPath* path= [NSIndexPath indexPathForRow:sender.tag-100 inSection:0];
UITableViewCell*cell=(UITableViewCell *)[tblView cellForRowAtIndexPath:path];
}
注意:您需要从UITableviewcell中删除addTarget和buttonClicked实现
在cellForRowAtIndexPath中,方法addtarget to button,在这个类中,方法implemnt buttonClicked.*

简单的解决方案是,当您在cellForRowAtIndexPath中配置单元格时,需要设置cell.button.tag=100+indexPath.row

-(void)btnClicked:(UIButton *)sender
{
NSIndexPath* path= [NSIndexPath indexPathForRow:sender.tag-100 inSection:0];
UITableViewCell*cell=(UITableViewCell *)[tblView cellForRowAtIndexPath:path];
}
注意:您需要从UITableviewcell中删除addTarget和buttonClicked实现
在cellForRowAtIndexPath方法addtarget to button中,在这个类中,实现buttonClicked方法。**

有很多方法。。但自iOS8以来,superView的方式不再可用。我正在分享一个在所有iOS中都能完美运行的代码

在cellForRow方法上编写此选择器

[cell.btnOnCell addTarget:self action:@selector(btnAction:event:) forControlEvents:UIControlEventTouchUpInside];
现在写下这个方法

-(void)btnAction: (UIButton *)sender event:(id)event
{
    NSSet *touches = [event allTouches];
    UITouch *touch = [touches anyObject];
    CGPoint currentTouchPosition = [touch locationInView:self.tblView];
    NSIndexPath *indexPath = [self.tblView indexPathForRowAtPoint:currentTouchPosition];
    NSLog(@"%li",(long)indexPath.row);
}

有很多方法可以做到这一点。。但自iOS8以来,superView的方式不再可用。我正在分享一个在所有iOS中都能完美运行的代码

在cellForRow方法上编写此选择器

[cell.btnOnCell addTarget:self action:@selector(btnAction:event:) forControlEvents:UIControlEventTouchUpInside];
现在写下这个方法

-(void)btnAction: (UIButton *)sender event:(id)event
{
    NSSet *touches = [event allTouches];
    UITouch *touch = [touches anyObject];
    CGPoint currentTouchPosition = [touch locationInView:self.tblView];
    NSIndexPath *indexPath = [self.tblView indexPathForRowAtPoint:currentTouchPosition];
    NSLog(@"%li",(long)indexPath.row);
}

有很多方法可以做到这一点。。但自iOS8以来,superView的方式不再可用。我正在分享一个在所有iOS中都能完美运行的代码

在cellForRow方法上编写此选择器

[cell.btnOnCell addTarget:self action:@selector(btnAction:event:) forControlEvents:UIControlEventTouchUpInside];
现在写下这个方法

-(void)btnAction: (UIButton *)sender event:(id)event
{
    NSSet *touches = [event allTouches];
    UITouch *touch = [touches anyObject];
    CGPoint currentTouchPosition = [touch locationInView:self.tblView];
    NSIndexPath *indexPath = [self.tblView indexPathForRowAtPoint:currentTouchPosition];
    NSLog(@"%li",(long)indexPath.row);
}

有很多方法可以做到这一点。。但自iOS8以来,superView的方式不再可用。我正在分享一个在所有iOS中都能完美运行的代码

在cellForRow方法上编写此选择器

[cell.btnOnCell addTarget:self action:@selector(btnAction:event:) forControlEvents:UIControlEventTouchUpInside];
现在写下这个方法

-(void)btnAction: (UIButton *)sender event:(id)event
{
    NSSet *touches = [event allTouches];
    UITouch *touch = [touches anyObject];
    CGPoint currentTouchPosition = [touch locationInView:self.tblView];
    NSIndexPath *indexPath = [self.tblView indexPathForRowAtPoint:currentTouchPosition];
    NSLog(@"%li",(long)indexPath.row);
}


“ButtonClicked”操作位于
UITableViewCell
文件中,因此我无法访问
UITableView
此处从tableviewcell中删除您的按钮单击操作,并将其添加到上述方法中,即我提到的“ButtonClicked”操作位于
UITableViewCell
文件中,因此,我无法访问
UITableView
此处从tableviewcell中删除您的按钮单击操作,并将其添加到上述方法中,我提到的“ButtonClicked”操作位于
UITableViewCell
文件中,因此,我无法访问
UITableView
此处从tableviewcell中删除您的按钮单击操作,并将其添加到上述方法中,我提到的“ButtonClicked”操作位于
UITableViewCell
文件中,因此,我无法访问此处的
UITableView
从tableviewcell中删除您的按钮单击操作,并将其添加到我提到的上述方法中为什么要执行
addTarget
?那我该怎么办呢?刚刚推出新控制器?我可以使用
prepareForSegue
btw吗?为什么要使用
addTarget
?那我该怎么办呢?刚刚推出新控制器?我可以使用
prepareForSegue
btw吗?为什么要使用
addTarget
?那我该怎么办呢?刚刚推出新控制器?我可以使用
prepareForSegue
btw吗?为什么要使用
addTarget
?那我该怎么办呢?刚刚推出新控制器?我可以使用
prepareForSegue
btw吗?然后我该怎么做?在视图控制器的属性中保存所选单元格的信息,并在
prepareforsgue
方法中将其传输到另一个视图控制器?还有,我为什么要使用
addTarget
而不是
@IBAction
方法呢?顺便说一句,我已经分享了一段代码来找出单击了哪个单元格的按钮。现在,这一切都取决于您的需求,您想用这个索引路径做什么。如果您不想编写选择器,iAction也会工作。好的,谢谢。在这种情况下,下一个问题是——我对你问题的详细回答……然后我该怎么办?在视图控制器的属性中保存所选单元格的信息,并在
prepareforsgue
方法中将其传输到另一个视图控制器?还有,我为什么要使用
addTarget
而不是
@IBAction
方法呢?顺便说一句,我已经分享了一段代码来找出单击了哪个单元格的按钮。现在,这一切都取决于您的需求,您想用这个索引路径做什么。如果您不想编写选择器,iAction也会工作。好的,谢谢。在这种情况下,下一个问题是——我对你问题的详细回答……A