UITableViewController select header for section

后端 未结 4 943
盖世英雄少女心
盖世英雄少女心 2020-12-24 11:47

I have a UITableView with multiple sections. Each section has a section header (a custom view) is there an easy way to detect when someone selects the section h

相关标签:
4条回答
  • 2020-12-24 12:12

    No there is no way to do it with the UITableViewDelegate.

    What you can do is to add a button the size of the section header view and add it to the view. Set the tag of the button to the section index. Then just add the UIViewController as a target for the UIControlEventTouchUpInside.

    Then by looking at the tag of the button you can see which section is clicked.

    0 讨论(0)
  • 2020-12-24 12:18

    This isn't radically different than @rckoenes answer, but it does provide a more orthodox way of handling events on views rather than using invisible buttons.

    I'd rather add a UITapGestureRecognizer to my header view instead of adding invisible buttons and resizing them:

    UITapGestureRecognizer *singleTapRecogniser = [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:)] autorelease];
    [singleTapRecogniser setDelegate:self];
    singleTapRecogniser.numberOfTouchesRequired = 1;
    singleTapRecogniser.numberOfTapsRequired = 1;   
    [yourHeaderView addGestureRecognizer:singleTapRecogniser];
    

    and then:

    - (void) handleGesture:(UIGestureRecognizer *)gestureRecognizer;
    

    You can use gesture.view to see which was touched. Then do whatever you need to do to find out which header it was (tags, data array lookup... )

    0 讨论(0)
  • 2020-12-24 12:28

    Here is what worked for me in Swift 2:

    override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let footerView = UITableViewHeaderFooterView()
        footerView.textLabel?.text = "Header Text"
        let tapRecognizer = UITapGestureRecognizer(target: self, action: #selector(handleTap))
        tapRecognizer.delegate = self
        tapRecognizer.numberOfTapsRequired = 1
        tapRecognizer.numberOfTouchesRequired = 1
        footerView.addGestureRecognizer(tapRecognizer)
        return footerView
    }
    
    @objc func handleTap(gestureRecognizer: UIGestureRecognizer) {
        print("Tapped")
    }
    
    0 讨论(0)
  • 2020-12-24 12:30

    This worked for evaluating the section and row. I hope it helps everybody else who is struggling to get this work properly...

    override func viewDidLoad() {
        let tapGesture = UITapGestureRecognizer(target: self, action: #selector(sectionTapped(sender:)))
        tableView?.addGestureRecognizer(tapGesture)
        tapGesture.delegate = self as? UIGestureRecognizerDelegate
    }
    
    @objc func sectionTapped(sender: UITapGestureRecognizer) {
        if sender.state == UIGestureRecognizerState.ended {
            guard let tableView = self.tableView else {
                return
            }
            if let view = sender.view {
                let tapLocation = sender.location(in: tableView)
                if let tapIndexPath = tableView.indexPathForRow(at: tapLocation) {              
                    if (tableView?.cellForRow(at: tapIndexPath) as? UITableViewCell) != nil {
                        // do something with the row
                        print("tapped on row at index: \(tapIndexPath.row)")
                    }
                }  else {
                    for i in 0..<tableView.numberOfSections {
                        let sectionHeaderArea = tableView.rectForHeader(inSection: i)
                        if sectionHeaderArea.contains(tapLocation) {
                            // do something with the section
                            print("tapped on section at index: \(i)")
                        }
                    }
                }
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题