Objective c 在tableview下添加按钮
我正在尝试以编程方式创建一个视图。我想要的结果是一个滚动视图,里面有一个tableview。在这个表视图下,我想添加一些按钮 我不知道该怎么做我试过这个但不起作用:Objective c 在tableview下添加按钮,objective-c,uitableview,uiview,uiscrollview,Objective C,Uitableview,Uiview,Uiscrollview,我正在尝试以编程方式创建一个视图。我想要的结果是一个滚动视图,里面有一个tableview。在这个表视图下,我想添加一些按钮 我不知道该怎么做我试过这个但不起作用: - (void)loadView { [super loadView]; tableView = [[UITableView alloc] initWithFrame:[[self view] bounds] style:UITableViewStyleGrouped]; [tableView setDel
- (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];
}
我遗漏了什么或做错了什么?需要注意的一点是,UITableView是UIScrollView的一个子类,因此您可能需要以不同的方式管理UITableView的大小,而不是让它进行滚动 您的代码似乎将tableView和deconnectButton设置为相同的大小,该大小是scrollView superview的大小。我认为这会造成tableView模糊按钮的效果 根据您描述的内容,听起来您应该根据表的内容计算表的大小,然后相应地设置其框架。然后将按钮的边框设置为刚好低于该边框。此外,还需要使用其contentSize属性设置scrollView的大小。这种情况下的问题是,您必须始终保持scrollView的大小和按钮的位置与tableView的大小同步
您可以研究将表中的最后一行设置为按钮并删除外部滚动视图。最后,这可能会减少代码。事实上,我找到了解决方案。tableview有一个名为tableFooterView的属性。你所要做的就是: -创建UIView -将按钮添加到此视图 -最后在TableFooter视图上设置它 代码如下:
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];
如果UITableView位于UINavigationController中,则可以在
UITableViewController
/UIViewController
的底部设置工具栏项目
UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:nil action:nil];
self.toolbarItems = @[barButton];
请记住按如下方式显示工具栏:
self.navigationController.toolbarHidden = NO;
//or animated
[self.navigationController setToolbarHidden:NO animated:YES];
这可能比在你的桌子下面黑一个视图更干净