【问题标题】:How to add two UIButtons in Tableview section header如何在 Tableview 部分标题中添加两个 UIButton
【发布时间】:2019-04-01 14:23:17
【问题描述】:

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

我尝试在 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

}

【问题讨论】:

  • 你能在你目前尝试过的内容上添加代码吗?

标签: ios swift uitableview uitableviewsectionheader


【解决方案1】:

你可以试试这个代码。

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

【讨论】:

    【解决方案2】:

    你有两个选择:

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

    【讨论】:

    • 是的,我确实喜欢这样,我在该视图中添加了两个按钮。那些没有得到点击事件。
    • 我会试试你的代码@Augusto 并尽快给你反馈
    • 我已经编辑了你的代码,只需复制和粘贴。
    • 使用情节提要的解决方案会更加健壮,只需创建一次整个标题视图并不时重用它。在处理计算帧时,自动布局会派上用场。
    • 工作正常@Augusto。并将右大括号添加到 button1 CGrect 行。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-01-14
    • 2015-11-26
    • 1970-01-01
    • 1970-01-01
    • 2015-07-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多