在 tableView 的 Section Header 中为 imageView 添加手势

Add gesture for the imageView in tableView's Section Header

我想实现一个 header 看起来像 this 的表格视图。我想实现一个功能,当我按下 header 中的图标时,它会转到另一个视图控制器。 但似乎手势识别器不起作用。识别器根本不会给出任何响应

header 的代码在这里

  override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let headerView = UITableViewHeaderFooterView(frame:CGRect(x: 0, y: 0, width: frameWidth, height: headerViewHeight))
        let iconOffset = CGPoint(x: 8, y: 8)
        let iconSize = CGSize(width: headerViewHeight-16, height: headerViewHeight-16)
        let imageView = UIImageView(frame: CGRect(origin: iconOffset, size: iconSize))
        imageView.image = UIImage(named: "HENRY")
        imageView.layer.cornerRadius = (headerViewHeight-16)/2
        imageView.layer.masksToBounds = true

        let tapRecognizer = UITapGestureRecognizer(target: self, action: Selector("iconTapGestureHandler:"))
//        tapRecognizer.cancelsTouchesInView = false
        tapRecognizer.numberOfTapsRequired = 1
        tapRecognizer.delegate = self
        imageView.addGestureRecognizer(tapRecognizer)

        headerView.addSubview(imageView)
        return headerView
}

然而,iconTapGestureHandler 函数不会在点击图像视图后被触发。

委托方法我也实现了

func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWithGestureRecognizer otherGestureRecognizer: UIGestureRecognizer) -> Bool {
    return true
}
func gestureRecognizerShouldBegin(gestureRecognizer: UIGestureRecognizer) -> Bool {
    return true
}

iconTapGestureHandler函数如下

@IBAction func iconTapGestureHandler(sender:UIGestureRecognizer){
    println("in touch")
    let mainPageVC = MainPageTableViewController()
    mainPageVC.isMyself = false
    mainPageVC.isPushed = false
    self.navigationController?.pushViewController(mainPageVC, animated: true)
}

默认情况下,在 UIImageView 中禁用用户交互。所以添加

imageView.userInteractionEnabled = true