how to retrieve all visible table section header views

后端 未结 10 555
渐次进展
渐次进展 2020-12-31 04:44

is there a way to get all the section header views that are visible?

something similar to UITableView\'s visibleCells instance method..

10条回答
  •  温柔的废话
    2020-12-31 05:05

    All of these answers turn out to be overly complicated. All you need to do is call headerView(forSection:) in a loop. It will return nil for header views that are not visible.

    However, there is a catch: Your header views have to inherit from UITableViewHeaderFooterView for this to work (which they technically should anyway, but everything except this works even if they don't so you can easily miss this.)

    Once you have your class inheriting correctly, you can just do this:

    for section in 0 ..< tableView.numberOfSections {
        guard let header = tableView.headerView(forSection: section) as? CustomHeaderView else { continue }
        header.update(...)
    }
    

提交回复
热议问题