views:

2303

answers:

2

Hello,

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?

+1  A: 

One thing to note is that UITableView is a subclass of UIScrollView so you will likely have to manage the size of the UITableView differently than if you were just letting it do the scrolling.

Your code appears to be setting the tableView and the deconnectButton to the same size and that size is the size of the scrollView superview. I would expect this to have the affect of the tableView obscuring the button.

Based on what you describe it sounds like you should calculate what the size of the table needs to be based on its contents and then set its frame accordingly. Then set the frame of the button to be just below that. Also, you will need to set the size of the scrollView using its contentSize property. The problem in this scenario is that you will have to always be keeping the size of the scrollView and the position of the button in sync with the size of the tableView.

You might investigate making the last row in the table the button and eliminating the outer scroll view. In the end that may result in less code.

Jon Steinmetz
A: 

Actually i found the solution. the tableview has a property named tableFooterView. All you have to do is to :

-Create a UIView -Add a button to this view -Finaly set it on the tableFooterView

Here is the code :

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