2009-07-24 150 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]; 

這可能比你的黑客如下表視圖清潔。

相關問題