当我将tableHeaderView添加到另一个subView并替换tableHeaderView时,为什么删除了tableHeaderView?

时间:2012-10-24 04:14:44

标签: objective-c xcode uitableview

这是我想要做的完整代码(只有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

保留

那么有人可以解释发生了什么吗?

我可以很容易地避开这个问题,但我想知道更多的实际情况。

1 个答案:

答案 0 :(得分:1)

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