我有一个包含 3 个部分的 UITableView。他们每个人都有一个我使用 viewForFooterInSection 添加的页脚。我遇到的问题是,当我向下滚动 tableview 时,页脚会粘在屏幕底部,并且不会像其他单元格一样滚动。有谁知道如何使页脚几乎像一个单元格一样,并与表格的其余部分一起滚动?谢谢!
5 回答
我真的想通了。可能不是最聪明的方法,但我将 UITableView 样式更改为分组,并且修复了它。我不得不稍微调整一下 TableView 以使单元格看起来与未分组时相同(背景清晰,没有分隔符),但效果很好。页脚不再粘在 TableView 的底部。
页脚应该粘住。诀窍是为每个部分添加一个额外的单元格并在那里呈现页脚。如果您需要更多帮助,请添加评论,但它应该非常简单。
编辑:
问:好的。我正在使用 Core Data 和 NSFetchedResultsController 来填充 TableView。这会让实现这一目标变得更加棘手吗?
答:一点也不。覆盖
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
在每个部分中添加一个额外的单元格,然后在
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
测试 indexPath.row > 是否比该部分的 fetchedResultsController 行。如果为真,请添加显示页脚信息的单元格。
解决此问题的一种方法是,如果将页脚设置为滚动视图中的最后一个单元格的单元格之一(可以完成,但将其设置为数组中的最后一项,您可以从中设置 uitable)
添加一个额外的单元格,使其不可见,并呈现您的视图,这不是添加页脚的可取方式。正确地做到这一点非常简单:
- (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: 如果你想像我在这里一样添加文本。
当我发现我的代码不适用于横向模式时,我确实遇到了类似的问题,而只能在纵向模式下工作。在我的旧代码中,当在横向时,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;
}