如何将目标添加到 tableview header 部分中的按钮

How to add target to button in tableview header section

所以我有一个 table 视图,其中有 2 个 header,每个视图有一个按钮。 我想知道 header 的哪个按钮被点击了,我该如何实现。

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    let view = UIView()
    view.backgroundColor = UIColor.dashboardScreenHeader

    let titleLabel = UILabel()
    titleLabel.frame = CGRect(x: 11, y: 12, width:170, height: 18)
    titleLabel.text = headerSection[section]
    titleLabel.font = titleLabel.font.withSize(16)

    let button = UIButton(type: .system)
    button.setTitle("See All", for: .normal)
    button.setTitleColor(UIColor.customBlueColor, for: .normal)
    button.titleLabel?.font = UIFont.boldSystemFont(ofSize: 14)
    button.frame = CGRect(x: 303, y: 14, width: 62, height: 12)
    button.addTarget(self, action: #selector(showDetail), for: .touchUpInside)

    view.addSubview(titleLabel)
    view.addSubview(button)

    return view
}

@objc func showDetail(){
    print("button tapped")
}

我尝试通过此显示详细信息按钮中的部分,但出现错误

我想在 showDetail 函数中实现这个

点击第 1 部分中的“查看全部”按钮 -> 执行 x

点击第 2 部分的“查看全部”按钮 -> 执行

您可以通过为每个按钮分配 标签 并检查按钮操作中的相同标签来实现此目的,如下所示:

在您的 viewForHeaderInSection 方法中:

button.tag = section
button.addTarget(self, action: #selector(showDetail:), for: .touchUpInside)

按钮操作:

@objc func showDetail(sender: UIButton!) {
  if(sender.tag == 0){
  //first button tapped
  }else if(sender.tag ==1){
  //second button tapped
  }
  .....
  .....
}

试试这个

 func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    let view = UIView()
    view.backgroundColor = UIColor.dashboardScreenHeader

    let titleLabel = UILabel()
    titleLabel.frame = CGRect(x: 11, y: 12, width:170, height: 18)
    titleLabel.text = headerSection[section]
    titleLabel.font = titleLabel.font.withSize(16)

    let button = UIButton(type: .system)
    button.setTitle("See All", for: .normal)
    button.setTitleColor(UIColor.customBlueColor, for: .normal)
    button.titleLabel?.font = UIFont.boldSystemFont(ofSize: 14)
    button.frame = CGRect(x: 303, y: 14, width: 62, height: 12)
    button.addTarget(self, action: #selector(showDetail(_:)), for: .touchUpInside)

    button.tag = indexPath.section

    view.addSubview(titleLabel)
    view.addSubview(button)

    return view
}

@objc func showDetail(_ button:UIButton){
    switch button.tag{
    case 0:
        //code for section 0
     case 1:
        //code for section 1
     default:
        //default code
    }

}

您需要为在 viewForHeaderInSection 中创建的每个按钮分配标签。

button.tag = section
button.addTarget(self, action: #selector(showDetail:), for: .touchUpInside)

然后

 @objc func showDetail(_ button:UIButton){
    switch button.tag{
     case 0:
        // code for section 1
        // Do your X
     case 1:
        // code for section 2
        // Do your Y
     default:
        //default code
    }
}

希望对您有所帮助!

在您的 viewForHeaderInSection 中将 tag 分配给按钮,

button.tag = section
button.addTarget(self, action: #selector(showDetail:), for: .touchUpInside)

func showDetail(sender: Any?) {
  guard let button = sender as? UIButton, let tag = button.tag
  if(tag == 0){

  } else if(tag == 1){

    }
}

可以自定义headerView,在headerView文件中,自定义样式。 "dequeueReusableHeaderFooterView" 可以优化代码和内存。