Home > OS >  How to to access button in TableView header in Swift?
How to to access button in TableView header in Swift?

Time:07-16

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

private var button = UIButton()

CodePudding user response:

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
}
  • Related