섹션 헤더보기의 가운데에 텍스트 레이블을 추가하기 위해 다음 코드를 작성했습니다.Swift에서 tableViewSection 헤더에 레이블과 UIButton을 추가하는 방법은 무엇입니까?
코드의 두 번째 절반은 너비가 100이고 섹션 머리글의 오른쪽 구석에 맞춰진 UIButton
을 추가하는 것입니다.
결과는 추가 된 레이블 만 가운데에 나타납니다. 머리글에 버튼이 전혀 표시되지 않습니다!
구현에서 어디서 잘못 될지 지적 해 주시겠습니까? 고맙습니다.
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let headerView = UIView()
// code for adding centered title
headerView.backgroundColor = UIColor.gray
let headerLabel = UILabel(frame: CGRect(x: 0, y: 0, width:
tableView.bounds.size.width, height: 28))
headerLabel.textColor = UIColor.black
headerLabel.text = titlesList[section]
headerLabel.textAlignment = .center
headerView.addSubview(headerLabel)
// code for adding button to right corner of section header
let showHideButton: UIButton = UIButton(frame: CGRect(x:headerView.frame.size.width - 100, y:0, width:100, height:28))
showHideButton.setTitle("Show Closed", for: .normal)
showHideButton.backgroundColor = UIColor.blue
showHideButton.addTarget(self, action: #selector(btnShowHideTapped), for: .touchUpInside)
headerView.addSubview(showHideButton)
return headerView
}