Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to to access button in TableView header in Swift?

I currently having trouble accessing a button in a tableView header section. My button is in the first header section of my tableview (which contains two headers, my button that I want to access is only in the first header). Here is an example of my current files. Basically, what I want to do is try to access the button in the first header.

TableView File

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    guard let vM = suggestionSections[safeIndex: section] else { return nil }
    let header = SectionHeaderView(frame: .zero)
    header.configure(vM)
    header.delegate = delegate
    return header
}

SectionHeaderView File

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

private var button = UIButton()

>Solution :

lazy var button = UIButton() //private remove

then in view for header in section

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    guard let vM = suggestionSections[safeIndex: section] else { return nil }
    let header = SectionHeaderView(frame: .zero)
    header.configure(vM)
    if section == 0 {
        header.button.backgroundColor = .red
    } else {
    //hide or do something with button
    }
    header.delegate = delegate
    return header
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading