Unexpected Header View in UITableView's section using NSFetchedResultsController

*爱你&永不变心* 提交于 2019-12-09 19:45:01

问题


It is easy to see, that the lightblue is a UITableViewHeaderFooterView, and the white one is UITableViewCell. I use NSFetchedResultsController to load sections and groups from CoreData. Every blue header is a new section.

SOMETIMES (not always) there is a header view instead of a table view cell. Why?

In viewDidLoad i register header View:

tableView.registerNib(UINib(nibName: "PBOUserWorkDayHeaderView", bundle: nil), forHeaderFooterViewReuseIdentifier: PBOUserWorkDayHeaderViewIdentifier)

What is more interesting, when I Debug View Hierarchy you can see, that there is my custom cell, just covered by header. Why?

Is it iOS bug? What do you think? Sth wrong with rendering or with my way of thinking?


回答1:


Since it is a problem with headers, I created a custom method to remove unnecessary headers every time when tableView ask for `viewForHeaderInSection:.

Assuming that you know how to distinguish the headers from other views (in my case they have a custom class) and that you know that header views are direct subviews of tableView you can remove them in following way:

private func removeDoubledHeaders() {
    let subviews = tableView.subviews.reverse()

    var dates = [String]()
    for view in subviews {
        if let header = view as? PBOUserWorkDayHeaderView {
            if let date = header.dateLabel.text {
                if contains(dates, date) {
                    view.removeFromSuperview()
                } else {
                    dates.append(date)
                }
            }
        }
    }
}


来源:https://stackoverflow.com/questions/30253785/unexpected-header-view-in-uitableviews-section-using-nsfetchedresultscontroller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!