如何在Tableview部分标题中添加两个UIButton

时间:2018-10-28 13:47:13

标签: ios swift uitableview uitableviewsectionheader

我添加了带有两个按钮的自定义tableview标头,但是按钮被禁用,无法进行控件事件。我想要这样的布局。我是开发新手。任何建议或解决方案  i want selectAll/Cancel and Send buttons in Tableview section header

我试图在ViewforHeaderSection函数的视图内部添加带有按钮的视图

func tableView(_ tableView:UITableView,viewForHeaderInSection部分: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

}

by implementing above code i'm getting like this

2 个答案:

答案 0 :(得分:1)

您有两个选择:

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

在这种情况下,创建yx时必须正确设置UIView(frame: CGRect())UIButton(frame: CGRect())的位置

编辑

从您的代码中,您只需要添加目标:

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

}

答案 1 :(得分:0)

您可以尝试使用此代码。

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