Add buttons under a tableview

前端 未结 3 1136
感动是毒
感动是毒 2021-02-11 02:27

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

相关标签:
3条回答
  • 2021-02-11 02:49

    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];
    
    0 讨论(0)
  • 2021-02-11 03:07

    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.

    0 讨论(0)
  • 2021-02-11 03:10

    If you have the UITableView inside a UINavigationController you can set toolbar items at the bottom on your UITableViewController / UIViewController.

    UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:nil action:nil];
    self.toolbarItems = @[barButton];
    

    Please remember to show the Toolbar as well like this:

    self.navigationController.toolbarHidden = NO;
    
    //or animated
    [self.navigationController setToolbarHidden:NO animated:YES];
    

    That is probably cleaner than hacking a view below your table.

    0 讨论(0)
提交回复
热议问题