How to make UITableView Header selectable?

后端 未结 12 1302
执笔经年
执笔经年 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: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")
    }
    

提交回复
热议问题