2009-07-24 10 views
2

プログラムでビューを作成しようとしています。私が持っている結果は、内部にテーブルビューを持つスクロールビューです。そして、私はこれを試したことをやって正確にどのように知りませんが、それは動作しません私はいくつかのボタンにテーブルビューでボタンを追加する

を追加したい、このテーブルビューの下で:

- (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という名前のプロパティがあります。あなたがしなければならないすべてはあるに:

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スーパービューのサイズです。私はこれがボタンを隠すtableViewの影響を持つと期待します。

説明に基づいて、テーブルのサイズに基づいてテーブルのサイズを計算し、それに応じてフレームを設定する必要があります。次に、ボタンのフレームをそのすぐ下に設定します。また、contentSizeプロパティを使用してscrollViewのサイズを設定する必要があります。このシナリオの問題は、常にscrollViewのサイズとボタンの位置をtableViewのサイズと同期して保持する必要があることです。

テーブルの最後の行をボタンにして、外側のスクロールビューを削除することを検討することがあります。結局、コードが少なくなる可能性があります。

1

あなたの場合:

のUIView -Add -FinalyがtableFooterViewここ

にそれを設定し、このビューにボタンのコードです-CreateあなたのUITableViewController/UIViewControllerの下部にツールバーの項目を設定することができますUINavigationController内のUITableViewを持っている。

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]; 

を。

関連する問題