2009-07-24 90 views
2

我正尝试以编程方式创建视图。我想要的结果是一个带有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]; 


} 

我缺少什么或者做错了什么?

回答

16

其实我找到了解决方案。 tableview有一个名为tableFooterView的属性。所有您需要做的是:

- 创建一个UIView - 添加一个按钮,这个观点 -Finaly设置在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]; 
2

有一点需要注意的是,UITableView是UIScrollView的子类,因此您可能必须以不同的方式管理UITableView的大小,而不仅仅是让它执行滚动。

您的代码似乎将tableView和deconnectButton设置为相同的大小,并且该大小是scrollView超级视图的大小。我希望这会产生影响按钮的隐藏功能。

根据您描述的内容,您应该计算出表格的大小需要根据其内容进行计算,然后相应地设置其框架。然后将该按钮的框架设置为低于该框架。此外,您将需要使用其contentSize属性来设置scrollView的大小。在这种情况下,问题是您必须始终保持scrollView的大小和按钮的位置与tableView的大小同步。

您可能会调查制作表格中最后一行的按钮并消除外滚动视图。最终可能导致代码少。

1

如果您在UINavigationController中有UITableView,您可以在您的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]; 

这可能比你的黑客如下表视图清洁。

相关问题