在tableview下添加按钮

Pti*_*taw 2 objective-c uitableview uiscrollview uiview

我正在尝试以编程方式创建视图.我想要的结果是一个滚动视图,里面有一个tableview.在这个表视图下我想添加一些按钮

我不知道该怎么做我试过这个但是它不起作用:

- (void)loadView {
    [super loadView];

    tableView = [[UITableView alloc] initWithFrame:[[self view] bounds] style:UITableViewStyleGrouped];
    [tableView setDelegate:self];
    [tableView setDataSource:self];

    scrollView = [[UIScrollView alloc] initWithFrame:[[self view] bounds]];
    //[scrollView setBackgroundColor:[UIColor blackColor]];
    [scrollView setBouncesZoom:YES];

    deconnectButton = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];
    [deconnectButton setTitle:@"Deconect" forState:UIControlStateNormal];
    [deconnectButton setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];

    //[deconnectButton addTarget:self action:action forControlEvents:UIControlEventTouchUpInside]; 
    deconnectButton.frame = tableView.frame;
    NSLog(@"Tableview frame : %@", NSStringFromCGRect(tableView.frame));

    [scrollView addSubview:deconnectButton];

    [scrollView addSubview:tableView];


    [[self view] addSubview:scrollView];


}
Run Code Online (Sandbox Code Playgroud)

我错过了什么或做错了什么?

Pti*_*taw 16

实际上我找到了解决方案.tableview有一个名为tableFooterView的属性.你所要做的就是:

- 创建一个UIView - 在此视图中添加一个按钮 - 最后将它设置在tableFooterView上

这是代码:

tableView = [[UITableView alloc] initWithFrame:[[self view] bounds] style:UITableViewStyleGrouped];
[tableView setDelegate:self];
[tableView setDataSource:self];

// create a UIButton (Deconnect button)
UIButton *btnDeco = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btnDeco.frame = CGRectMake(0, 0, 280, 40);
[btnDeco setTitle:@"Déconnecter" forState:UIControlStateNormal];
btnDeco.backgroundColor = [UIColor clearColor];
[btnDeco setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
[btnDeco addTarget:self action:@selector(deconnect:) forControlEvents:UIControlEventTouchUpInside];

// create a UIButton (Change pseudo button)
UIButton *btnChange = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btnChange.frame = CGRectMake(0, 50, 280, 40);
[btnChange setTitle:@"Changer Pseudo" forState:UIControlStateNormal];
btnChange.backgroundColor = [UIColor clearColor];
[btnChange setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
[btnChange addTarget:self action:@selector(changePseudo:) forControlEvents:UIControlEventTouchUpInside];


//create a footer view on the bottom of the tabeview
UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(20, 0, 280, 100)];
[footerView addSubview:btnDeco];
[footerView addSubview:btnChange];

tableView.tableFooterView = footerView; 
[footerView release];

[[self view] addSubview:tableView];
Run Code Online (Sandbox Code Playgroud)