如何在 Tableview 部分添加两个 UIButtons header

How to add two UIButtons in Tableview section header

我添加了带有两个按钮的自定义表格视图header,但是按钮被禁用,无法进行控制事件。我想要这样的布局。我是开发新手。任何建议或解决方案

我尝试在 ViewforHeaderSection 函数中添加带有按钮的视图

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

    let inviteSectionHeaderview  = UIView.init(frame: CGRect(x:0, y:0, width: self.view.bounds.width, height: self.view.bounds.height))
    let selectAllBtn = UIButton.init(frame:CGRect(x:16, y: inviteSectionHeaderview.bounds.height/2, width:130, height:20))
    let sendButton = UIButton.init(frame:CGRect(x:inviteSectionHeaderview.bounds.width - 30, y: inviteSectionHeaderview.bounds.height/2, width:60, height:20))
    selectAllBtn.setTitle("select all/Cancel", for: .normal)
    selectAllBtn.backgroundColor = .black
    sendButton.backgroundColor = .black
    sendButton.setTitle("SEND", for: .normal)
    self.contactsTable.addSubview(inviteSectionHeaderview)
    inviteSectionHeaderview.addSubview(selectAllBtn)
    inviteSectionHeaderview.addSubview(sendButton)
    return inviteSectionHeaderview

}

您有两个选择:

  1. 在故事板中创建您的 UIView
  2. 以编程方式创建

选项 2

  1. 创建您的UIView

    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? 
        let headerView = UIView(frame: CGRect(x: 0, y: 0, width: tableView.frame.width, height: 30.0))        
    
        // Button1
        let button1 = UIButton(frame: CGRect(x: 15.0, y: 0, width: 100, height: 28.0)
        button1.setTitle("Button 1", for: .normal)
        button1.addTarget(self, action: #selector(selectorButton1), for: .touchUpInside)
    
        // Button2
        let button2 = UIButton(frame: CGRect(x: tableView.frame.width-150, y: 0, width: 150, height: 30.0))
        button2.setTitle("Button2", for: .normal)
        button2.addTarget(self, action: #selector(selectorButton2), for: .touchUpInside)
        button2.semanticContentAttribute = UIApplication.shared
            .userInterfaceLayoutDirection == .rightToLeft ? .forceLeftToRight : .forceRightToLeft
    
        headerView.addSubview(button1)
        headerView.addSubview(button2)
        return headerView 
    }
    
    
        @objc func selectorButton1(_ sender : Any) {
    
        }
    
        @objc func selectorButton2(_ sender : Any) {
    
        }
    

在这种情况下,您必须在创建UIView(frame: CGRect())UIButton(frame: CGRect())

时正确设置yx位置

编辑

根据您的代码,您只需添加目标:

selectAllBtn.addTarget(self, action: #selector(selectorAllBtn), for: .touchUpInside)
sendButton.addTarget(self, action: #selector(selectorSendButton), for: .touchUpInside)

@objc func selectorAllBtn(_ sender : Any) {

}

@objc func selectorSendButton(_ sender : Any) {

}

你可以试试这个代码。

 func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        var headerView = tableView.dequeueReusableHeaderFooterView(withIdentifier: "headerView")

        if headerView == nil {
            headerView = UITableViewHeaderFooterView(reuseIdentifier: "headerView")

            let button1 = UIButton(frame: CGRect(x: 8, y: 8, width: 80, height: 40))
            button1.setTitle("Select", for: .normal)
            button1.addTarget(self, action: #selector(self.buttonAction), for: .touchUpInside)
            headerView?.addSubview(button1)

        }

        return headerView
    }

    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 50 // set a height for header view
    }

    @objc func buttonAction(_ sender: UIButton) {
     // write your code...
    }