需要在iOS中的UITable的tableheaderview中添加多个UI元素

时间:2013-01-08 17:09:22

标签: ios uitableview uibutton uilabel nstableheaderview

我有一个具有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中吗?

2 个答案:

答案 0 :(得分:3)

创建一个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

答案 1 :(得分:1)

由于tableHeaderViewUIView,请尝试将其添加到addSubview:(UIView*)或创建UIView,然后将其添加为tableHeaderView