how to retrieve all visible table section header views

后端 未结 10 582
渐次进展
渐次进展 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:00

    A UITableView extension that gives you the visibleHeaderViews based on @OhadM answer (https://stackoverflow.com/a/45525595/5058116), but which checks on potential nil values.

    extension UITableView {
    
        /// The section header views that are visible in the table view.
        var visibleHeaderViews: [UITableViewHeaderFooterView] {
    
            var headerViews = [UITableViewHeaderFooterView]()
    
            guard let indexPaths = indexPathsForVisibleRows else { return headerViews }
    
            for indexPath in indexPaths {
                if let headerView = headerView(forSection: indexPath.section) {
                    headerViews.append(headerView)
                }
            }
    
            return headerViews
    
        }
    
    }
    

提交回复
热议问题