如何在TableView标头部分的按钮中添加目标

时间:2018-12-26 07:29:19

标签: ios uitableview swift4

所以我有一个带有2个标题的表格视图,每个标题都有一个按钮。 我想知道点击了标题的哪个按钮,我将如何实现。

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部分的“全部”按钮->做y

5 个答案:

答案 0 :(得分:3)

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

在您的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
  }
  .....
  .....
}

答案 1 :(得分:2)

您需要为在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
    }
}

希望获得帮助!

答案 2 :(得分:1)

在您的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){

    }
}

答案 3 :(得分:0)

尝试

 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
    }

}

答案 4 :(得分:0)

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

相关问题