ios UITableView 页脚,停止浮动在内容上

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5740518/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 19:39:11  来源:igfitidea点击:

UITableView Footer, Stop from floating over content

iosuitableviewuikit

提问by Jason

I would like to make my UITableView Footer stop floating over my content, as is the default activity. I want my footer to be.. well.. a footer. Always the last view to be displayed at the end of my tableview. :)

我想让我的 UITableView 页脚停止浮动在我的内容上,就像默认活动一样。我希望我的页脚是......好吧......一个页脚。始终显示在我的 tableview 末尾的最后一个视图。:)

Same thing with the header too actually.

实际上,标题也同样如此。

Is there any easy way to disable it from floating over top of the table view as you scroll?

有什么简单的方法可以禁用它在滚动时漂浮在表格视图的顶部吗?

Thank you everyone, I couldn't find an answer elsewhere.

谢谢大家,我在其他地方找不到答案。

回答by Jason

Ok, it turns out that if you set the footer view via blahblahtableView.tableFooterView = someview;, the footer will not scroll with the table.

好的,事实证明,如果您通过 设置页脚视图blahblahtableView.tableFooterView = someview;,页脚将不会随表滚动。

However, if you create your footer using viewForFooterInSection, it WILL follow your view around and stick on the bottom of the screen.

但是,如果您使用 来创建页脚viewForFooterInSection,它将跟随您的视图并粘在屏幕底部。

Not sure why I couldn't find this answer sooner. Sorry all :)

不知道为什么我找不到这个答案更快。对不起大家:)

回答by rsc

You have 2 types of footer: one is the section footerand the other is the tableview footer.

您有两种类型的页脚:一种是节页脚,另一种是tableview 页脚

The section footerappears below of each section. On the other side, the tableview footerappears just once at the end of the tableview.

部分页脚出现以下各部分的。在另一边,实现代码如下页脚在刚刚出现一次的tableview中结束

There is no 1-click way to disable the floating of the section footer.

没有一键式禁用节页脚浮动的方法。

If you want a "footer" that always appears as the last cell of its section and does get fixed on the bottom of the tableview you need to add an extra cell to your section and always treat it as the last one of your section.

如果您想要一个始终显示为其部分的最后一个单元格并且确实固定在 tableview 底部的“页脚”,您需要向您的部分添加一个额外的单元格并始终将其视为您部分的最后一个。

If you just have 1 section in your tableview, you could use the tableview footer to be your footer.

如果您的 tableview 中只有 1 个部分,则可以使用 tableview 页脚作为页脚。

回答by guogangj

- (id)init {
    self = [self initWithStyle:UITableViewStyleGrouped]; //Default is UITableViewStylePlain
    return self;
}

回答by Zulqarnain

self.YOUR_TABLE_VIEW.tableFooterView = YOUR_FOOTER_VIEW;

回答by megu5202

If you have your cells divided into sections and are using the function:

如果您将单元格分成多个部分并使用该函数:

func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView?

to set your footer, then in IB you could also set the UITableView Style to Groupedis another possible solution!

要设置页脚,然后在 IB 中,您还可以将 UITableView 样式设置为Grouped是另一种可能的解决方案!

回答by Lucas Chwe

Checkout my answer here, https://stackoverflow.com/a/41816730/1553351. The code does exactly what you described in your question

在这里查看我的答案,https://stackoverflow.com/a/41816730/1553351。该代码完全符合您在问题中的描述

回答by Henri Normak

If I got the question right, you basically want to fix the footer/header in place and scroll everything else? If so you could just have the header and the footer as separate views from the tableview, that rest above the tableview in the view hierarchy, so they cover the contents of the tableview.

如果我的问题是对的,您基本上是想将页脚/页眉固定到位并滚动其他所有内容?如果是这样,您可以将页眉和页脚作为与 tableview 分开的视图,它们位于视图层次结构中的 tableview 上方,因此它们覆盖了 tableview 的内容。

If you want them to bounce (when scrolling above or below the contents) with the tableview, then you need to track the tableview's method (tableview is a subclass of scrollview, so it works) - (void)scrollViewDidScroll:and calculate the new position to either the footer or the header in there.

如果您希望它们与 tableview 一起反弹(在内容上方或下方滚动时),那么您需要跟踪 tableview 的方法(tableview 是 scrollview 的子类,因此它可以工作)- (void)scrollViewDidScroll:并计算到页脚或页脚的新位置头在那里。

I hope you get my answer, I have done something similar to this and it worked (I wanted the "Release to update" kind of thingy on top of the tableview)

我希望你能得到我的答案,我已经做了类似的事情并且它起作用了(我想要在 tableview 顶部的“发布更新”类型的东西)

回答by Abolfoooud

I was having a similar problem. I was using only a footer to indicate the number of cell in the table at the bottom of it. But it was not scrolling with the table cels as i expected (its was visible at all times on top of the table until i reach the bottom where it bounces off).

我遇到了类似的问题。我只使用页脚来指示表格底部的单元格数量。但它并没有像我预期的那样随着表格 cels 滚动(它一直在表格顶部可见,直到我到达它反弹的底部)。

My solution to overcome this was to create 2 sections, one contains the table cells and another empty BUT with a header. This header will show the counter and will behave as i wanted (to remain at the bottom of the table at all times rather than overlaying the cells).

我的解决方案是创建 2 个部分,一个包含表格单元格,另一个包含标题的空 BUT。此标题将显示计数器并按照我的意愿运行(始终保持在表格底部而不是覆盖单元格)。

The recommended solution of adding a dummy cell at the bottom was not useful in my case at it would be bordered with the table wired-border and i did not want that.

在底部添加虚拟单元格的推荐解决方案在我的情况下没有用,因为它会与表格有线边框接壤,而我不想要那样。

Hope this helps

希望这可以帮助

回答by Exile3daime

#pragma mark -- There are two options to do so --

#pragma mark -- 有两个选项可以这样做 --

  1. Use UITableView delegate

    effect: Floating footer on a table view

  1. 使用 UITableView 委托

    效果:表格视图上的浮动页脚

-(CGFloat)tableView:(UITableView *)tableView
heightForFooterInSection:(NSInteger)section{
    return HEIGHT;
}
-(UIView*)tableView:(UITableView *)tableView
viewForFooterInSection:(NSInteger)section{
    return FOOTERVIEW
}
  1. Use setter of table view

    effect: Attached at the bottom of last cell

  1. 使用表视图的setter

    效果:附加在最后一个单元格的底部

theTableView.tableFooterView = YOURFOOTERVIEW

// Do not set this delegate
// or you would get a extra blank at the bottom of the table

//  -(CGFloat)tableView:(UITableView *)tableView
//  heightForFooterInSection:(NSInteger)section{
//    return HEIGHT;
//  }