1

这是我想做的完整代码(只有 2 行)

[self.headerViewofWholeTable addSubview:self.delegate.tableView.tableHeaderView];
self.delegate.tableView.tableHeaderView= self.headerViewofWholeTable;

事情不起作用,所以我开始添加打印信息

[self.headerViewofWholeTable addSubview:self.delegate.tableView.tableHeaderView];
PO(self.headerViewofWholeTable.subviews);
self.delegate.tableView.tableHeaderView= self.headerViewofWholeTable;
PO(self.headerViewofWholeTable.subviews);

简单的 4 行代码:D

结果:

 self.headerViewofWholeTable.subviews: (
        "<UILabel: 0x8bf9770; frame = (50 15; 250 21); text = 'Pull Down to Refresh'; clipsToBounds = YES; opaque = NO; autoresize = RM+BM; userInteractionEnabled = NO; layer = <CALayer: 0x8bf97e0>>",
        "<UILabel: 0x8b63aa0; frame = (50 35; 257 20); text = 'last updated'; clipsToBounds = YES; opaque = NO; autoresize = RM+BM; userInteractionEnabled = NO; layer = <CALayer: 0x8b63b10>>",
        "<UIView: 0x8b63b80; frame = (20 11; 22 54); autoresize = RM+BM; layer = <CALayer: 0x8bf6c40>>",
        "<UIImageView: 0x8be9ca0; frame = (0 0; 320 10); autoresize = LM+RM+BM; userInteractionEnabled = NO; layer = <CALayer: 0x8be9ce0>> - shading-top-Table.png"
    )
 self.headerViewofWholeTable.subviews: (
        "<UILabel: 0x8bf9770; frame = (50 15; 250 21); text = 'Pull Down to Refresh'; clipsToBounds = YES; opaque = NO; autoresize = RM+BM; userInteractionEnabled = NO; layer = <CALayer: 0x8bf97e0>>",
        "<UILabel: 0x8b63aa0; frame = (50 35; 257 20); text = 'last updated'; clipsToBounds = YES; opaque = NO; autoresize = RM+BM; userInteractionEnabled = NO; layer = <CALayer: 0x8b63b10>>",
        "<UIView: 0x8b63b80; frame = (20 11; 22 54); autoresize = RM+BM; layer = <CALayer: 0x8bf6c40>>"
    )

所以你看,

self.delegate.tableView.tableHeaderView= self.headerViewofWholeTable; 删除 self.delegate.tableView.tableHeaderView 指向的视图。然而,观点

"<UIImageView: 0x8be9ca0; frame = (0 0; 320 10); autoresize = LM+RM+BM; userInteractionEnabled = NO; layer = <CALayer: 0x8be9ce0>> - shading-top-Table.png"

不应该消失,因为它由 self.headerViewofWholeTable 保留

那么任何人都可以解释发生了什么吗?

我可以很容易地绕过这个问题,但我想知道更多实际发生的事情。

4

1 回答 1

1

UITableView setTableHeaderView方法的实现(您在第二行中调用)很可能假定当前标题视图仍然是表的子视图。因此,当您尝试分配新的标题视图时,表格视图首先从其父视图中删除当前标题(不知道它已被移动到您的其他视图)。表格视图并不期望其标题视图已移动到新视图。因此,它最终会从您的视图中删除标题headerViewofWholeTable视图。

于 2012-10-24T05:00:45.180 回答