当内容视图中的图像被触摸时,防止UITableViewCell高亮



我在UITableViewCell的内容视图右侧添加了一个UIImageView

我试图防止在用户触摸UIImageView时突出显示单元格(因为附加的手势识别器执行一些其他功能)。

我考虑过实现tableView:shouldHighlightRowAtIndexPath:并返回NO,但我不确定如何在调用此方法时轻松确定图像是否被触摸。有简单的方法吗?

试试这个:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ...
    // Add tap gesture recogniser to cell's image view
    UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapped)];
    tap.cancelsTouchesInView = YES;
    cell.theImageView.userInteractionEnabled = YES;
    [cell.theImageView addGestureRecognizer:tap];
    return cell;
}

重要的部分是确保图像视图有userInteractionEnabled = YES,手势识别器有cancelsTouchesInView = YES

这将导致tapped方法在点击图像视图时触发,但是单元格不会被高亮显示。

为什么不添加一个自定义类型的UIButton呢?会有点击按钮动作。不影响单元格高亮

防止表格单元格高亮

cell.selectionStyle = UITableViewCellSelectionStyleNone;

[cell setSelectionStyle:UITableViewCellSelectionStyleNone];

最新更新