0

I have added footerview programmatically for each section as follows. I could able to see the footerview.

However, when I scroll up or down at the bottom or at the top of the tableview, footerview overlays on top of tableviewcells.

How could I able to disable it?

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    if(adminOrderElements[section].expanded && [adminOrderElements[section].notes length]>0)
    {
        return 60;
    } else {
        return 0;
    }

    return 60;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView *footer = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 60)];
    footer.backgroundColor = [UIColor clearColor];

    UILabel *lbl = [[UILabel alloc]initWithFrame:footer.frame];
    lbl.backgroundColor = [UIColor clearColor];
    lbl.text = @"Your Text";
    lbl.textAlignment = NSTextAlignmentCenter;
    [footer addSubview:lbl];

    return footer;
}

Before Scroll

enter image description here

After Scroll

enter image description here

casillas
  • 16,351
  • 19
  • 115
  • 215
  • What height is your `heightForFooterInSection` returning? 60 or 0? – Paulw11 Aug 01 '17 at 03:21
  • If there is a `footerview`, it is `60`. if not, then it is `0` based on the `if(adminOrderElements[section].expanded && [adminOrderElements[section].notes length]>0)` – casillas Aug 01 '17 at 03:27
  • 1
    I would suggest adding a breakpoint on else part where you are returning 0 and check whether it goes there and I think you dont need second return 60 statement – 3stud1ant3 Aug 01 '17 at 03:30
  • @Paulw11 and @user1000 If you check the first image, `Order 4` does not have any content to show on the `footerview`, that is why there is no footer view, in other words the height is `0`. But `Order 3` has some content, therefore, the height of footer view is `60` and you could able to see label (`Your Text`) there. – casillas Aug 01 '17 at 03:31
  • 1
    I think your code is fine , please give the color say blue to section footer , and scroll , you will see its working fine and its not overlapping its supposed to be there, please check – 3stud1ant3 Aug 01 '17 at 03:37
  • What do you think about this ?https://stackoverflow.com/questions/5740518/uitableview-footer-stop-from-floating-over-content – casillas Aug 01 '17 at 03:38
  • You are asking to make `footer.backgroundColor = [UIColor clearColor];` to `footer.backgroundColor = [UIColor blueColor];` and test? – casillas Aug 01 '17 at 03:39
  • Do you require your footer to be the end of the table or end of the section?And yes please change the color to blue – 3stud1ant3 Aug 01 '17 at 03:40
  • I want to add `footerview` for `each section` not for entire `tableview`. – casillas Aug 01 '17 at 03:41
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/150672/discussion-between-user1000-and-hotspring). – 3stud1ant3 Aug 01 '17 at 03:41
  • By design table cells will go "under" the footer; You need to use an opaque colour for your footer – Paulw11 Aug 01 '17 at 03:45
  • could you please illustrate with code? – casillas Aug 01 '17 at 03:52
  • As per your code I think you are returning a new UIView object as footerview for each section even for those section for which you dont want to display footerview and thats the problem. So, I think you should return empty view when footer is not there, so you can also conditions to viewForFooterInSection for checking if you want to show footerview or not and then return foorterview object or nil. – Paras Gorasiya Aug 01 '17 at 04:35

1 Answers1

2

If I understand your question correctly, you only have to change the UITableViewStyle to UITableViewStyleGrouped. According to the docs, for table views using grouped style the section headers and footers do not float.

phi
  • 10,634
  • 6
  • 53
  • 88