IOS - UITableView Cell,点击单元格内的UIImageView但停止调用didSelectRowAtIndexPath

时间:2015-08-12 14:10:14

标签: ios uitableview

我有一个自定义单元格,当您点击自定义单元格时,会出现另一个视图,但在此自定义单元格中,我有一个Flag ImageView。我需要做的是,如果轻触Flag ImageView,请不要转到didSelectRowAtIndexPath,因为我需要将Flag ImageView更改为红色标记,并在单击Flag ImageView时停止显示View。所以几乎像Javascript你使用stopPropagation(),有没有办法在IOS中使用自定义单元格?

3 个答案:

答案 0 :(得分:4)

您需要设置userinteraction enable yes。 设置

cell.imageView.userInteractionEnabled = YES;

然后它不会调用tableview的didselect方法

答案 1 :(得分:0)

如果您在单元格内部有UIImageView,则无法直接停止它。因此,您可以使用自定义单元格而不是UIImageView来获取UIButton。这将允许您解决didSelectRowAtIndexPath中的问题。

因此,当我们点击UIButton时,它将优先于Button优先于UITableViewCell Row。如果这对您不起作用,那么请提供您的反馈意见,我会尽快解决您的问题。

答案 2 :(得分:0)

使用UIButton作为标志。为此按钮创建IBOutlet并将背景设置为您想要的背景。

在界面构建器中,您需要为按钮分配一个标签号,然后在Tableview中将cellForRowAtIndexPath实例化为它。

    UIButton *favs = (UIButton *)[cell viewWithTag:105];

然后在cellForRowAtIndexPath方法中实现目标操作以及按钮,如:

[favs addTarget:self action:@selector(checkButtonTapped:event:) forControlEvents:UIControlEventTouchUpInside];

然后为checkButtonTapped实现一个方法,传递触摸事件,如:

    - (void)checkButtonTapped:(id)sender event:(id)event{

    //Create touch set
    UITouch *touch = [[event allTouches] anyObject];

    //Get Current touch position
    CGPoint currentTouchPosition = [touch locationInView:self.tableView];
    //Get IndexPath
    NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint: currentTouchPosition];
}

然后执行任何操作,使用触摸点中的indexPath将Unbutton的背景更改为红色标记。