让“didSelectRowAtIndexPath”不只在单元格内的图像被点击时调用(但以其他方式调用)?

Bla*_*ard 0 uitableview didselectrowatindexpath ios swift

我想在我的 上放置一个图像UITableViewCell,并在用户点击单元格时将单元格移动到详细信息视图控制器。但是,当用户点击仅放在单元格部分的图像时,我也想让单元格不移动到细节 VC。该单元格如下所示:

[cell.textLabel     myImage    accessoryType] <- UITableViewCell
Run Code Online (Sandbox Code Playgroud)

但是,当我像下面这样实现我的代码时,当用户点击单元格时,她必须移动到细节 VC,即使她点击图像覆盖的微小部分。didSelectRowAtIndexPath:当她点击图像时是否可以禁用?

tableView:didSelectRowAtIndexPath:

var cell = self.tableView.dequeueReusableCellWithIdentifier(CellReuseIdentifier, forIndexPath: indexPath) as! UITableViewCell

var list = listArray[indexPath.row]
cell.textLabel?.text =  list.name

cell.accessoryType = UITableViewCellAccessoryType.DetailButton
let image = UIImage(named: "myImage.png")
var imageView = UIImageView(image: image)
imageView.frame = CGRectMake(280, 4, 36, 36)
cell.addSubview(imageView)

return cell
Run Code Online (Sandbox Code Playgroud)

我试过了,imageView.userInteractionEnabled = false但结果没有改变。

另请注意,图像不是附件视图,因为我也想使用附件类型(两者都不能使用),因此您不能使用tableView:accessoryButtonTappedForRowWithIndexPath:委托方法。

Kle*_*son 5

您可以通过三种方法来实现它:

  1. 添加UITapGestureRecognizer到您的UIImageView. 如果你点击 ,它会强制UITableView不要调用。didSelectRowAtIndexPathUIImageView

  2. 如果您正在使用故事板,则可以覆盖override func shouldPerformSegueWithIdentifier(identifier: String!, sender: AnyObject!) -> Bool并避免使用它。

  3. 或者正如@DuncanLowrie 所说:

    使用 UIButton 而不是图像视图,或者用按钮覆盖图像。该按钮不必执行任何操作,但会拦截触摸。