【问题标题】:UITapGesture in UITableview headerUITableview 标头中的 UITapGesture
【发布时间】:2015-03-26 09:27:19
【问题描述】:

我试图在 swift 中使用可扩展的 tableview。所以我在表格视图的标题中添加了手势,但它无法识别。请指导我。只需交叉检查我的编码即可。

我的编码如下:

func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {

        let vvv = tableView.headerViewForSection(section)

        let tap = UITapGestureRecognizer(target: self, action: Selector("handleTap:"))
        tap.delegate = self
        vvv?.addGestureRecognizer(tap)
        return vvv
    }


    func handleTap(gestureRecognizer: UITapGestureRecognizer)
    {
        if(gestureRecognizer.state == .Ended)
        {
            println("SECTION PRESSED") //NOT ENTERING TO THIS BLOCK
        }     
    }

【问题讨论】:

  • viewForHeaderInSection 方法被调用了吗?
  • 用户交互是否开启?我还没有使用手势识别器完成它,但我记得使用自定义 UIView 子类并覆盖 touchesBegan 方法,然后告诉委托该视图已被点击。
  • 大家好! viewForHeaderInSection,这个方法被调用。但是,没有回应。如何解决?
  • 你好主!你是怎么做到的?你能解释一下吗?

标签: uitableview swift ios8 uigesturerecognizer


【解决方案1】:

在 Swift 4 中,您可以将 UITapGestureRecognizer 添加到标题视图:

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {

    let headerView = UIView()

    let tapRecognizer = UITapGestureRecognizer(target: self, action: #selector(headerTap))
    headerView.addGestureRecognizer(tapRecognizer)

    return headerView
}

@objc func headerTap() {
    // Tap action
}

【讨论】:

    【解决方案2】:

    我是这样做的。

    首先,创建一个带有“headerCellSection”变量的 UITableViewCell 类(或者任何你想调用它的东西)。

    import UIKit
    
    class HeaderCellTableViewCell: UITableViewCell {
    
        var headerCellSection:Int?
    
        override func awakeFromNib() {
            super.awakeFromNib()
        }
    
        override func setSelected(selected: Bool, animated: Bool) {
            super.setSelected(selected, animated: animated)
        }
    }
    

    将单元格添加到“viewForHeaderInSection”:

    func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
            let headerCell = tableView.dequeueReusableCellWithIdentifier("headerCell") as! HeaderCellTableViewCell
    
            return headerCell
        }
    

    将单元格的部分发送到先前声明的变量,并将点击手势识别器添加到单元格。代码应如下所示:

    func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let headerCell = tableView.dequeueReusableCellWithIdentifier("headerCell") as! HeaderCellTableViewCell
    
            // Send section
            headerCell.headerCellSection = section
    
            // Add gesture
            let headerTapGesture = UITapGestureRecognizer()
            headerTapGesture.addTarget(self, action: "myAction:")
            headerCell.addGestureRecognizer(headerTapGesture)
    
        return headerCell
    }
    

    然后添加动作。在操作中,您可以访问具有点击手势目标的视图。在那里您可以访问游览“headerCellSection”值,瞧!

    func myAction (sender: UITapGestureRecognizer) {
    
        // Get the view
        let senderView = sender.view as! HeaderCellTableViewCell
    
        // Get the section
        let section = senderView.headerCellSection
    
        print(section)
    }
    

    【讨论】:

      【解决方案3】:

      我通过将 UITapGestureRecognizer 添加到 headerView 的 contentView 来做到这一点。示例:

      headerCell.contentView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(headerTapped)))

      【讨论】:

        【解决方案4】:

        我知道这是一个有点老的问题来回答。但是没有一个答案被接受,我将给出另一个简单的解决方案。

        阅读原始问题后,我认为作者不想要自定义创建的部分标题。这就是他尝试使用以下代码的原因。

        let vvv = tableView.headerViewForSection(section)
        

        但我相信你会在上面的代码 sn-p 中得到一个空值 vvv。如果用户想访问默认视图,他可以这样做。

        tableView.register(UITableViewHeaderFooterView.self, forHeaderFooterViewReuseIdentifier: sectionHeaderIdentifier)
        

        在注册之后,我在 viewDidLoad() 函数中做了。然后使用下面的代码

        func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        
            let sectionView: UITableViewHeaderFooterView = tableView.dequeueReusableHeaderFooterView(withIdentifier: sectionHeaderIdentifier)!
            let tapGesture = UITapGestureRecognizer(target: self, action: #selector(sectionHeaderTapped))
            sectionView.addGestureRecognizer(tapGesture)
        
            return sectionView
        }
        

        上述函数将为您的表格部分标题创建默认视图,并启用点击手势属性。

        【讨论】:

          猜你喜欢
          • 2020-07-02
          • 1970-01-01
          • 1970-01-01
          • 2016-08-30
          • 2011-06-30
          • 1970-01-01
          • 2018-01-30
          • 1970-01-01
          • 2015-10-15
          相关资源
          最近更新 更多