How to add two UIButtons in Tableview section header

好久不见. 提交于 2021-02-10 17:30:38

问题


I added custom tableview header with two buttons, but buttons are disabled , unable to make control events. i want to get layout like this. i'm new to development. any suggestions or solution

i tried to add view with buttons inside view in ViewforHeaderSection function

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:


You have two options:

  1. Create your UIView in storyboard
  2. Create programatically

Option 2

  1. Create your 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) {
    
        }
    

In this case, you must set correctely y and x positions when create UIView(frame: CGRect()) and UIButton(frame: CGRect())

EDIT

From your code, you just need add the targets:

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) {

}



回答2:


You can try this code.

 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...
    }


来源:https://stackoverflow.com/questions/53032194/how-to-add-two-uibuttons-in-tableview-section-header

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!