이 문제가 있습니다. 도와 주실 수 있기를 바랍니다.
이것은 내가 가지고있는 코드입니다 viewForHeaderInSection
.
let button = UIButton(frame: CGRect(x: view.frame.width - (cancel.frame.width + xImage.frame.width + 100), y: view.frame.origin.y, width: cancel.frame.width + xImage.frame.width + 120, height: view.frame.height))
button.backgroundColor = UIColor.blue.withAlphaComponent(0.5)
button.addTarget(self, action: #selector(action), for: .touchUpInside)
view.addSubview(button)
그리고 이것은 action
func입니다.
@objc func action(sender: UIButton) {
let position: CGPoint = sender.convert(CGPoint.zero, to: self.tableView)
if let indexPath = self.tableView.indexPathForRow(at: position) {
let section = indexPath.section
print(section)
}
}
이제 첫 번째 헤더 (헤더 0)를 제외한 모든 헤더를 탭하면 올바른 섹션이 인쇄됩니다. 헤더 0도 감지하도록 만드는 방법은 무엇입니까?
버튼의 섹션을 알고 싶다면 섹션을 버튼의 태그에 할당 할 수 있습니다. 예 :
당신의 viewForHeaderInSection
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let button = UIButton(frame: CGRect(x: view.frame.width - (cancel.frame.width + xImage.frame.width + 100), y: view.frame.origin.y, width: cancel.frame.width + xImage.frame.width + 120, height: view.frame.height))
button.backgroundColor = UIColor.blue.withAlphaComponent(0.5)
button.addTarget(self, action: #selector(action), for: .touchUpInside)
view.addSubview(button)
button.tag = section // this is the trick
}
그리고 액션 내부 :
@objc func action(sender: UIButton) {
let section = sender.tag
print(section)
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다