iOS - viewForFooterInSection坚持UITableView的底部

时间:2012-07-15 15:42:43

标签: objective-c ios cocoa-touch uitableview uiview

我有一个包含3个部分的UITableView。他们每个都有一个我使用viewForFooterInSection添加的页脚。我遇到的问题是,当我向下滚动桌面视图时,页脚会粘到屏幕的底部,而不会像其他单元格那样滚动。有没有人知道如何制作它,所以页脚几乎就像一个单元格,并与表格的其余部分一起滚动?谢谢!

5 个答案:

答案 0 :(得分:10)

我实际上已经明白了。可能不是最聪明的方法,但我将我的UITableView样式改为分组,并修复了它。我不得不稍微调整一下TableView,这样细胞看起来就像它们没有分组一样(背景清晰,没有分隔符),但它工作正常。页脚不再粘在TableView的底部。

答案 1 :(得分:5)

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

编辑:

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

A:完全没有。覆盖

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

在每个部分和

中添加额外的单元格

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

测试indexPath.row>比你的fetchedResultsController的那一行的行。如果为true,请在显示页脚信息的单元格中添加。

答案 2 :(得分:4)

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

答案 3 :(得分:1)

当我发现我的代码无法使用横向模式时,我确实遇到了类似的问题,并且仅在纵向模式下工作。在我的旧代码中,当在横向时,tableview无法滚动低于可见视图,并且在滚动时它会弹回到顶行(不让我看到下面的行)。我需要改变的是确保将高度设置为44作为默认单元格高度。所以我的页脚基本上是另一个带有clearColor的单元格。请注意,我的应用使用' 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;
}

答案 4 :(得分:0)

添加一个额外的单元格,使其不可见,并渲染您的视图并不是添加页脚的可行方法。正确地做到这一点非常简单:

- (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:如果你想添加我在这里的文字。

相关问题