iOS - viewForFooterInSection sticking to bottom of

2019-03-27 19:19发布

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!

5条回答
我命由我不由天
2楼-- · 2019-03-27 19:21

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.

查看更多
劫难
3楼-- · 2019-03-27 19:24

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)

查看更多
闹够了就滚
4楼-- · 2019-03-27 19:28

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.

查看更多
虎瘦雄心在
5楼-- · 2019-03-27 19:29

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.

查看更多
兄弟一词,经得起流年.
6楼-- · 2019-03-27 19:38

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;
}
查看更多
登录 后发表回答