简体   繁体   English

iOS-viewForFooterInSection粘在UITableView的底部

[英]iOS - viewForFooterInSection sticking to bottom of UITableView

I have a UITableView with 3 sections. 我有一个包含3个部分的UITableView。 Each of them have a footer that I've added using viewForFooterInSection. 他们每个人都有一个页脚,我已使用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. 我遇到的问题是,当我向下滚动tableview时,页脚会停留在屏幕底部,而不会像其余单元格一样滚动。 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! 谢谢!

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. 也许这不是最聪明的方法,但我将UITableView样式更改为分组,并进行了修复。 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. 我必须对TableView进行一些调整,以使单元格看起来与未分组的单元格相同(背景清晰,没有分隔符),但是效果很好。 The footer no longer sticks to the bottom of the TableView. 页脚不再停留在TableView的底部。

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. 我正在使用Core Data和NSFetchedResultsController填充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. 测试indexPath.row>是否大于该部分的fetchedResultsController行。 If true, add in your cell that shows the footer information. 如果为true,则添加显示页脚信息的单元格。

解决此问题的一种方法是,如果将页脚设置为滚动视图中最后一个单元格中的一个单元格(可以完成,但是将其设置为从中设置uitable的数组中的最后一项)

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. 我要做的就是确保将高度设置为默认的单元格高度44。 So my footer is basically another cell with clearColor. 所以我的页脚基本上是另一个带有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;
}

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. 您还必须实现tableView:titleForFooterInSection:如果要添加文本,就像我在这里一样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM