iOS - viewForFooterInSection sticking to bottom of UITableView

15,713

Solution 1

I actually figured it out. Probably isn't the smartest way to do it, but I changed my UITableView style to grouped, and that fixed it. I had to tweak the TableView a bit so the cells would look the same as they did non-grouped (clear background, no separators), but it worked fine. The footer no longer sticks to the bottom of the TableView.

Solution 2

Footers are supposed to stick. The trick is to add an extra cell to each section and render the footer there. If you need more help, add a comment, but it should be pretty straightforward.

EDITS:

Q: Alright. I'm using Core Data and NSFetchedResultsController, to populate the TableView. Would that make it more tricky to accomplish this?

A: Not at all. Override

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

To add an extra cell in each section, and in

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

test if the indexPath.row > than your fetchedResultsController's rows for that section. If true, add in your cell that shows the footer information.

Solution 3

One way around this is if you set the footer as one of the cells as the last cell in the scroll view (could be done but setting it as the last item in the array that you set the uitable from)

Solution 4

I did had similar problem when I found out that my code did not work with Landscape mode, and only worked in Portrait mode. In my old code, when in Landscape the tableview can not scroll lower than visible view and it bounced back to top row when scroll (not letting me see the lower rows). All I have to change is to make sure that the height set to 44 as default cell height. So my footer is basically another cell with clearColor. Note my app uses 'AutoLayout'

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
  CGRect screenBounds = [UIScreen mainScreen].bounds;
  CGFloat width = screenBounds.size.width;
  UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, width, 44)];
  view.backgroundColor = [UIColor clearColor];

  UILabel *label = [[UILabel alloc] initWithFrame:view.frame];
  label.text = @"Your Text";
  [view addSubview:label];
  return view;
}

Solution 5

Adding an extra cell, making it invisible, and rendering your view there is not an advisable way of adding a footer. Doing it properly is pretty straight-forward:

- (UIView*)tableView:(UITableView*)tableView viewForFooterInSection:(NSInteger)section {
    NSString* sectionFooter = [self tableView:tableView titleForFooterInSection:section];

    UIView* view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, yourWidth, yourHeight)]; //create a view- the width should usually be the width of the screen
    UILabel* label = [[UILabel alloc] initWithFrame:someFrame]; 

    label.backgroundColor = [UIColor blueColor];
    label.textColor = [UIColor whiteColor];
    label.text = sectionFooter;

    [view addSubview:label];

    return view;
}

You will also have to implement tableView: titleForFooterInSection: if you want to add text like I have here.

Share:
15,713
mhbdr
Author by

mhbdr

Updated on June 12, 2022

Comments

  • mhbdr
    mhbdr almost 2 years

    I have a UITableView with 3 sections. Each of them have a footer that I've added using viewForFooterInSection. The problem I'm having is that when I scroll the tableview down, the footer sticks to the bottom of the screen, and doesn't scroll like the rest of the cells. Does anyone know how to make it so the footer almost acts like a cell, and scrolls along with the rest of the table? Thanks!

  • mhbdr
    mhbdr almost 12 years
    Alright. I'm using Core Data and NSFetchedResultsController, to populate the TableView. Would that make it more tricky to accomplish this?
  • Andrew Zimmer
    Andrew Zimmer almost 12 years
    Not at all. Override - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section To add an extra cell in each section, and - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath test if the indexPath.row > than your fetchedresultscontroller's rows for that section. If true, add in your cell that shows the footer information.
  • rsc
    rsc about 9 years
    Your answer works for adding a footer, but that footer will get sticked at the bottom of the tableview, exactly what he did not want.
  • Abdul Yasin
    Abdul Yasin about 7 years
    Thanks dude... Additionally i need to add section height.. Otherwise works fine..
  • Anthony Wang
    Anthony Wang almost 7 years
    To hide the section header that gets shown in the "Grouped" UITableView style, see stackoverflow.com/questions/19056428/…