iOS – viewForFooterInSection粘在UITableView的底部

我有3个部分的UITableView。 他们每个人都有一个使用viewForFooterInSection添加的页脚。 我遇到的问题是,当我向下滚动tableview时,页脚粘到屏幕的底部,不像其他单元格一样滚动。 有谁知道如何做到这一点,页脚几乎就像一个单元格,并与桌子的其余部分一起滚动? 谢谢!

我其实已经明白了 可能不是最明智的做法,但我改变了我的UITableView风格分组,并修复它。 我不得不调整一下TableView,所以单元看起来和非分组(清晰的背景,没有分隔符)看起来一样,但是工作正常。 页脚不再粘在TableView的底部。

页脚应该坚持。 诀窍是给每个部分添加一个额外的单元格,并在那里渲染页脚。 如果您需要更多帮助,请添加评论,但应该非常简单。

EDITS:

问:好的。 我正在使用Core Data和NSFetchedResultsController来填充TableView。 这样做会更棘手吗?

答:一点也不。 覆盖

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

在每个部分和中添加一个额外的单元格

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

testingindexPath.row>是否比你的fetchedResultsController的那一行的行更好。 如果为true,则添加显示页脚信息的单元格。

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

当我发现我的代码不适用于横向模式,并且只能在纵向模式下工作时,我遇到了类似的问题。 在我以前的代码中,当在横向时,tableview不能滚动到比可见视图更低的位置,并且在滚动时(不让我看到下面的行)弹回到顶部行。 所有我必须改变是确保高度设置为44作为默认的单元格高度。 所以我的页脚基本上是clearColor的另一个单元格。 请注意我的应用程序使用“自动布局”

 - (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; } 

添加一个额外的单元格,使其不可见,并呈现您的视图不是一个明智的方式添加页脚。 正确的做法非常简单:

 - (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; } 

你也必须实现tableView:titleForFooterInSection:如果你想像我这里添加文本。