How to make UITableView Header selectable?

后端 未结 12 1282
执笔经年
执笔经年 2020-12-16 11:49

I have made a custom section-header for UITableView, that includes some controls like segmented control, UIImageView ,etc. It successfully appears, but it\'s not tappable so

相关标签:
12条回答
  • 2020-12-16 12:18

    Simple drop in solution for Swift 3.0

     func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    
            let v = UITableViewHeaderFooterView()
            let tapRecognizer = UITapGestureRecognizer(target: self, action: #selector(handleTap))
            v.addGestureRecognizer(tapRecognizer)
            return v
        }
    
    
        func handleTap(gestureRecognizer: UIGestureRecognizer)
        {
            controller?.view.endEditing(true)
        }
    
    0 讨论(0)
  • 2020-12-16 12:19

    If anyone needs a solution that can work when the tableview has multiple sections, I had the same problem and the solution I came up with was to create different targets on a button at the viewForHeaderInSection method, based on the section. I am using a button but this should work just as well with UITapGestureRecognizer

    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    
            // ...
    
            if section == 0 {
                cell.button.addTarget(self, action: #selector(firstSectionTapped(_:)), for: .touchUpInside)
            } else if section == 1 {
                cell.button.addTarget(self, action: #selector(secondSectionTapped(_:)), for: .touchUpInside)
            }
    
            // ...
    
    }
    

    Then in the ViewController:

    @objc func goalCategoryTapped(_ sender: UITapGestureRecognizer?) {
        print("Section One Tapped")
    }
    
    @objc func ideaCategoryTapped(_ sender: UITapGestureRecognizer?) {
        print("Section Two Tapped")
    }
    
    0 讨论(0)
  • 2020-12-16 12:23

    My implementation in Swift:

    In your UITableViewController:

    let myView = MyView()
    
    let tapRecognizer = UITapGestureRecognizer(target: myView, action: "handleTap")
    myView.addGestureRecognizer(tapRecognizer)
    
    self.tableView.tableHeaderView = myView
    

    In MyView:

    class MyView: UIView {
    
        func handleTap() {
            // Handle touch event here
        }
    }
    
    0 讨论(0)
  • 2020-12-16 12:26

    Create a custom section headerView.. and then add Tap gesture to it.

    UITapGestureRecognizer * recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
    
    //This method will be called on Tap of section header
    - (void)handleTap:(UITapGestureRecognizer *)sender {
    //Do the action on Tap
    }
    
    0 讨论(0)
  • 2020-12-16 12:27

    If you need to know the index of the tapped section, you can use the following pattern (Swift 4) :

    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let view = UIView(frame: CGRect(x: 0, y: 0, width: 300, height: 44))
    
        // Configure your view here
        // ...
    
        let tapGestureRecognizer = UITapGestureRecognizer(
            target: self,
            action: #selector(headerTapped(_:))
        )
        view.tag = section
        view.addGestureRecognizer(tapGestureRecognizer)
        return view
    }
    
    @objc func headerTapped(_ sender: UITapGestureRecognizer?) {
        guard let section = sender?.view?.tag else { return }
    
        // Use your section index here
        // ...
    }
    
    0 讨论(0)
  • 2020-12-16 12:29

    swift solution for this:

    let tapR : UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "gotoHeaderArticle")
    tapR.delegate = self
    tapR.numberOfTapsRequired = 1
    tapR.numberOfTouchesRequired = 1
    yourView.addGestureRecognizer(tapR)
    

    Then implement

    func gotoHeaderArticle() {
    
    }
    

    Make sure to make your calling viewcontroller adhere to UIGestureRecognizerDelegate

    0 讨论(0)
提交回复
热议问题