【问题标题】:Add Subview to self.tableView.tableHeaderView does not work将 Subview 添加到 self.tableView.tableHeaderView 不起作用
【发布时间】:2012-01-06 16:15:53
【问题描述】:

我有一个UITableViewController,我想扩展该表的tableHeaderView

所以在viewDidLoad 我添加了以下内容:

self.tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, self.tableView.frame.size.width, 60.0f)];

很好,效果很好 - 我得到了我的标题视图。

现在我想在该视图的顶部添加一个按钮。所以我添加以下代码:

UIButton *someButton = [[UIButton alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 100.0f, 20.0f)];
[someButton setTitle:@"Filter" forState:UIControlStateNormal];

[self.tableView.tableHeaderView addSubview:someButton];

但该按钮不会出现在tableHeaderView 上。当我使用 UILabel 而不是 UIButton 运行相同的代码时,我可以看到标签。

我的错误在哪里?

【问题讨论】:

  • 请注意:上述代码的第一行将导致非 ARC 应用程序发生泄漏。仅供复制和粘贴此代码的任何人参考。

标签: ios uitableview uiview


【解决方案1】:

看看这个 SO 问题:UIButton - alloc initWithFrame: vs. buttonWithType:

我建议你使用UIButton *someButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 或类似的东西。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-15
    • 1970-01-01
    • 2011-12-15
    • 2016-02-26
    • 2015-02-27
    • 2019-05-17
    相关资源
    最近更新 更多