如何从UIButton单元格项目iOS调用didDeselectRowAtIndexPath

时间:2014-05-20 06:47:26

标签: ios iphone objective-c uibutton

我的自定义单元格有UIButtonUITextField。现在,当我点击按钮时,我想要捕获indexpath.row中的didDeselectRowAtIndexPath。但是当我点击按钮时didDeselectRowAtIndexPath没有被调用。我知道按钮会捕获点击。但很想知道任何解决方法吗?任何聪明的人。

请指导

感谢。

enter image description here

2 个答案:

答案 0 :(得分:0)

中添加按钮目标
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

     //Continue with your code...

 [cell.actionButton addTarget:self action:@selector(didTapButton:) forControlEvents:UIControlEventTouchUpInside];    
}


- (void)didTapButton:(id)sender {
    // Cast Sender to UIButton
    UIButton *button = (UIButton *)sender;

    // Find Point in Superview
    CGPoint pointInSuperview = [button.superview convertPoint:button.center toView:self.tableView];

    // Infer Index Path
    NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:pointInSuperview];

    NSLog(@"%d", indexPath.row);
}

答案 1 :(得分:0)

无需从按钮点击事件调用didSelect方法尝试进入- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath索引,只需设置按钮标记 indexPath.row ,如:

     cell.btnInvite.tag=indexPath.row;
     [cell.btnInvite addTarget:self action:@selector(btnCommentClick:) forControlEvents:UIControlEventTouchUpInside];


-(void)btnCommentClick:(UIButton*)sender
{
  NSLog(@"frined add %d",sender.tag); //now do you stuff that you want you have selected cell index as a button tag.

}