使用 NSFetchedResultsController 的 UITableView 部分中的意外标题视图 [英] Unexpected Header View in UITableView's section using NSFetchedResultsController

查看:16
本文介绍了使用 NSFetchedResultsController 的 UITableView 部分中的意外标题视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

容易看出,浅蓝色的是UITableViewHeaderFooterView,白色的是UITableViewCell.我使用 NSFetchedResultsController 从 CoreData 加载部分和组.每个蓝色标题都是一个新部分.

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?

viewDidLoad 中,我注册了标题视图:

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?

是iOS错误吗?你怎么看?渲染或我的思维方式有什么问题?

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

推荐答案

由于是 headers 的问题,所以我创建了一个自定义方法来在每次 tableView 请求 `viewForHeaderInSection 时删除不必要的 headers:.

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

假设您知道如何将标题与其他视图区分开来(在我的例子中它们有一个自定义类)并且您知道标题视图直接子视图tableView 您可以通过以下方式删除它们:

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)
                }
            }
        }
    }
}

这篇关于使用 NSFetchedResultsController 的 UITableView 部分中的意外标题视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆