Как добавить цель к кнопке в разделе заголовка таблицы - PullRequest
0 голосов
/ 26 декабря 2018

Итак, у меня есть табличное представление с 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 -> do x

Нажата кнопка «Все» в разделе 2 -> do y

Ответы [ 5 ]

0 голосов
/ 26 декабря 2018

Вы можете настроить headerView, в файле headerView, настроить стиль."dequeueReusableHeaderFooterView" может оптимизировать код и память.

0 голосов
/ 26 декабря 2018

В вашем 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){

    }
}
0 голосов
/ 26 декабря 2018

попробуйте

 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
    }

}
0 голосов
/ 26 декабря 2018

Вам необходимо присвоить тег каждой кнопке, которую вы создали в 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
    }
}

Надеюсь, эта помощь!

0 голосов
/ 26 декабря 2018

Этого можно добиться, назначив тег каждой кнопке и проверив наличие одного и того же тега в действии кнопки, например:

В вашем методе 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
  }
  .....
  .....
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...