【问题标题】:Need to add multiple UI elements to tableheaderview of UITable in iOSiOS中UITable的tableheaderview需要添加多个UI元素
【发布时间】:2012-12-22 14:58:45
【问题描述】:

我有一个具有 tableView 的 UITableViewController。对于这个 tableView,我想添加一个标签和一个按钮,但不幸的是,我只能添加一个或另一个。

这是我正在使用的代码:

- (void) viewDidLoad {

    [super viewDidLoad];

    selectAllButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [selectAllButton setFrame:CGRectMake(280, 0, 25, 25)];
    [selectAllButton setImage:[UIImage imageNamed:@"CheckBox1.png"] forState:UIControlStateSelected];
    [selectAllButton setImage:[UIImage imageNamed:@"CheckBox2.png"] forState:UIControlStateNormal];
    [selectAllButton setUserInteractionEnabled:YES];
    [selectAllButton addTarget:self action:@selector(buttonTouched:) forControlEvents:UIControlEventTouchUpInside];

    selectAllLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 300, 40)];
    [selectAllLabel setTextColor:[UIColor blackColor]];
    [selectAllLabel setBackgroundColor:[UIColor clearColor]];
    [selectAllLabel setText:@"Select All"];

    self.tableView.tableHeaderView = selectAllButton;
    self.tableView.tableHeaderView = selectAllLabel;

}

当我运行上述方法时,我只能看到我的标签,它告诉我它正在替换按钮。有没有办法让两个元素都出现在 tableheaderview 中?

【问题讨论】:

    标签: ios uitableview uibutton uilabel nstableheaderview


    【解决方案1】:

    由于tableHeaderViewUIView,请尝试使用addSubview:(UIView*) 添加它或创建一个UIView 并将其添加为您的tableHeaderView

    【讨论】:

      【解决方案2】:

      创建一个 UIView,将您的按钮和标签作为子视图添加到其中,而不是将该视图设置为您的 self.tableview.tableHeaderView

      ...your code
      UIView *headerView = [UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, selectAllButton.frame.size.height + selectAllLabel.frame.size.height)];
      
      [headerView addSubview:selectAllButton];
      [headerView selectAllLabel];
      
      self.tableView.tableHeaderView = headerView;
      

      当您初始化按钮和标签时​​,请务必更改框架,因为它们将基于 UIView

      【讨论】:

      • 非常感谢您的及时解决!
      猜你喜欢
      • 1970-01-01
      • 2016-01-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-19
      • 2012-01-26
      • 2017-06-19
      • 2015-11-05
      相关资源
      最近更新 更多