简体   繁体   English

我在UITableView中的标题视图未添加到UIView层次结构中

[英]My header view in UITableView is not added to the UIView Hierarchy

Using UITableView and NSFetchedResultsController I refresh and display the content of my table. 使用UITableViewNSFetchedResultsController刷新并显示表的内容。 At some point I hava a screen without header view. 在某些时候,我的屏幕没有标题视图。 It should be instead of the space between cells. 它应该代替单元格之间的空间。

在此处输入图片说明

When I debug View hierarchy I have the following info. 当我调试视图层次结构时,我有以下信息。 Selected cell in UI View hierarchy is appriopriate to the selected cell in Debug View Hierarchy . UI View层次结构中的选定单元格与Debug View Hierarchy中的选定单元格适当。 在此处输入图片说明

在此处输入图片说明

To create a header view I use following method: 要创建标题视图,我使用以下方法:

override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    let workday = workDayObjectForSection(section)
    let cell = tableView.dequeueReusableCellWithIdentifier(PBOUserWorkDayCellIdentifier) as! PBOUserWorkDayTableViewCell
    cell.configureCellWithWorkDay(workday)

    return cell
}

You shouldn't get view for tableView header using dequeueReusableCellWithIdentifier method. 你不应该让viewtableView使用头dequeueReusableCellWithIdentifier方法。 From the documentation : 文档中

If a cell object is reusable you assign it a reuse identifier (an arbitrary string) in the storyboard. 如果单元对象是可重用的,则在情节提要中为其分配重用标识符(任意字符串)。 At runtime, the table view stores cell objects in an internal queue. 在运行时,表视图将单元格对象存储在内部队列中。 When the table view asks the data source to configure a cell object for display, the data source can access the queued object by sending a dequeueReusableCellWithIdentifier: message to the table view, passing in a reuse identifier 当表视图要求数据源配置要显示的单元格对象时,数据源可以通过向表视图发送dequeueReusableCellWithIdentifier:消息并传递重用标识符来访问排队的对象

That means when you return header as reuse cell, tableView might take it do display in another place as cell. 这意味着当您将标题作为重用单元格返回时, tableView可能会将其作为单元格显示在另一个位置。 Note that behavior of header is different than cell, it stays at the top while section is visible. 请注意,标头的行为与单元格不同,它位于顶部,而部分可见。

However, you may use same view for header but create it from xib , storyboard etc every time and do not assign any reuse identifier. 但是,您可以对标题使用相同的视图,但是每次都通过xibstoryboard等创建它,并且不分配任何重用标识符。 And if you looking for some optimization, you can store them in some array and return cached values. 而且,如果您要进行一些优化,则可以将它们存储在某个数组中并返回缓存的值。 But I would like to recommend not to use subclasses of UITableViewCell as table header views 但我建议不要将UITableViewCell子类用作表头视图

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM