2

I'm trying to create a UITableView with multiple sections. Each section has it's own header, but I am trying to make a universal footer for the entire table view that stays in one position...

Is this logic possible using the UITableViewDelegate methods? Or should I create a custom view and just try and add it as a subview to my table view? The footer I currently have contains a UIButton.

If anyone has some sample code that would be great.

Edit: This question is not the same as the one referenced. I am trying to make a universal footer that floats above the UITableView. The other question does not specify the location of the footer, only that a footer is desired.

Johnny Gamez
  • 259
  • 6
  • 19

2 Answers2

4

read about UITableView's tableFooterView property.

And now some code: (using ARC)

UILabel *footer = [UILabel alloc] init];
footer.text = @"Some text" ;
footer.backgroundColor = [UIColor greenColor];
self.tableView.tableFooterView = footer;

Now at the bottom of entire tableview there is a UILabel that is green with some text.

John
  • 2,640
  • 1
  • 16
  • 16
  • This is almost what I'm looking for... I tried this out and it added a footer at the very bottom of the table view. I would like the footer to stay "floating" in the same positon (right above the `UITabBarController` selections. My table view will most likely have 3-4 sections with 5-10 rows in each section so I don't want the user to have to scroll through all that crap when they need to tap the `UIButton` to segue to the next view. – Johnny Gamez Feb 27 '13 at 06:32
  • I know no way to do that with the method I gave you. I think you will have to layout something in code or IB. Full Screen View that contains a tableview and a UIView locked to the bottom of the FullScreeView. Then the table would be set to hold the rest of the area. – John Feb 27 '13 at 14:14
1

I ended up creating a subview and adding my button to that view. I then made that view my "footer".

Here is the code that gave me the desired results.

- (void)viewDidLoad
{

[super viewDidLoad];

//self.tableView.delegate = self;
//self.tableView.dataSource = self;

//save current tableview, then replace view with a regular uiview
self.tableView = (UITableView*)self.view;
UIView *replacementView = [[UIView alloc] initWithFrame:self.tableView.frame];
self.view = replacementView;
[self.view addSubview:self.tableView];

UIView *footerView  = [[UIView alloc] initWithFrame:CGRectMake(0, 370, 320, 45)];

//create the button
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
//button.userInteractionEnabled = YES;

//the button should be as big as a table view cell
//width of the button can be set but the width of the view it is added to will always match the width of the tableView
[button setFrame:CGRectMake(60, 0, 200, 45)]; 

//set title, font size and font color
[button setTitle:@"Build" forState:UIControlStateNormal];
[button.titleLabel setFont:[UIFont boldSystemFontOfSize:20]];   
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

//set action of the button
[button addTarget:self action:@selector(buildThenSegue)
 forControlEvents:UIControlEventTouchUpInside];

//add the button to the view
[footerView addSubview:button];
footerView.userInteractionEnabled = YES;

[self.view addSubview:footerView];
self.tableView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);

}

Take note that this is in a subclass of UITableViewController.

I referenced this answer to another question: https://stackoverflow.com/a/9084267/1091868

Community
  • 1
  • 1
Johnny Gamez
  • 259
  • 6
  • 19