【问题标题】:iOS - viewForFooterInSection sticking to bottom of UITableViewiOS - viewForFooterInSection 粘在 UITableView 的底部
【发布时间】:2012-07-14 15:33:13
【问题描述】:

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

【问题讨论】:

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


    【解决方案1】:

    当我发现我的代码不能在横向模式下工作时,我确实遇到了类似的问题,而只能在纵向模式下工作。在我的旧代码中,当在横向时,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;
    }
    

    【讨论】:

      【解决方案2】:

      我真的想通了。可能不是最聪明的方法,但我将 UITableView 样式更改为分组,并且修复了它。我不得不稍微调整一下 TableView 以使单元格看起来与未分组的单元格相同(背景清晰,没有分隔符),但效果很好。页脚不再粘在 TableView 的底部。

      【讨论】:

      【解决方案3】:

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

      编辑:

      问:好的。我正在使用 Core Data 和 NSFetchedResultsController 来填充 TableView。这会让实现这一目标变得更加棘手吗?

      答:完全没有。覆盖

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

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

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

      测试 indexPath.row > 是否比该部分的 fetchedResultsController 行。如果为 true,请添加显示页脚信息的单元格。

      【讨论】:

      • 好的。我正在使用 Core Data 和 NSFetchedResultsController 来填充 TableView。这会让实现这一目标变得更加棘手吗?
      • 一点也不。覆盖 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 在每个部分中添加一个额外的单元格,并且 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 测试是否indexPath.row > 该部分的 fetchedresultscontroller 行。如果为真,请添加显示页脚信息的单元格。
      【解决方案4】:

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

      - (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: 如果你想像我在这里一样添加文本。

      【讨论】:

      • 您的答案适用于添加页脚,但该页脚会卡在表格视图的底部,这正是他不想要的。
      【解决方案5】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-05-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-11-27
        • 1970-01-01
        • 2012-10-01
        • 2013-09-12
        相关资源
        最近更新 更多