我的自定义单元格有UIButton
和UITextField
。现在,当我点击按钮时,我想要捕获indexpath.row
中的didDeselectRowAtIndexPath
。但是当我点击按钮时didDeselectRowAtIndexPath
没有被调用。我知道按钮会捕获点击。但很想知道任何解决方法吗?任何聪明的人。
请指导
感谢。
答案 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.
}