本文介绍了在 tableview 下添加按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试以编程方式创建视图.我想要的结果是一个带有 tableview 的滚动视图.在这个表格视图下我想添加一些按钮
I'm trying to create a view programmatically. The result that i want to have is a scroll view with a tableview inside. And under this table view i want to add some buttons
我不知道该怎么做,我试过了,但它不起作用:
I don't know exactly how to do that i tried this but it doesn't work :
- (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];
}
我错过了什么或做错了什么?
What am i missing or doing wrong?
推荐答案
其实我找到了解决方案.tableview 有一个名为 tableFooterView 的属性.你所要做的就是:
Actually i found the solution. the tableview has a property named tableFooterView. All you have to do is to :
-创建一个 UIView- 在此视图中添加一个按钮- 最后将其设置在 tableFooterView 上
-Create a UIView-Add a button to this view-Finaly set it on the 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];
这篇关于在 tableview 下添加按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!