ios 如何在 UITableView 中隐藏第一节标题(分组样式)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19056428/
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
How to hide first section header in UITableView (grouped style)
提问by Codo
As the design of table views using the grouped style changed considerably with iOS 7, I would like to hide (or remove) the first section header. So far I haven't managed to achieve it.
由于使用分组样式的表视图设计在 iOS 7 中发生了很大变化,我想隐藏(或删除)第一节标题。到目前为止,我还没有设法实现它。
Somewhat simplified, my code looks like this:
稍微简化一下,我的代码如下所示:
- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
if (section == 0)
return 0.0f;
return 32.0f;
}
- (UIView*) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
if (section == 0) {
UIView* view = [[UIView alloc] initWithFrame: CGRectMake(0.0f, 0.0f, 640.0f, 0.0f)];
return view;
}
return nil;
}
- (NSString*) tableView:(UITableView *) tableView titleForHeaderInSection:(NSInteger)section
{
if (section == 0) {
return nil;
} else {
// return some string here ...
}
}
If I return a height of 0, the other two methods will never be called with the section index 0. Yet an empty section header is still drawn with the default height. (In iOS 6, the two methods are called. However, the visible result is the same.)
如果我返回 0 的高度,则其他两个方法将永远不会以部分索引 0 被调用。但仍会使用默认高度绘制一个空的部分标题。(在 iOS 6 中,调用了这两个方法。但是,可见结果是一样的。)
If I return a different value, the section header gets the specified height.
如果我返回不同的值,部分标题将获得指定的高度。
If I return 0.01, it's almost correct. However, when I turn on "Color Misaligned Images" in the simulator, it marks all table view cells (which seems to be a logical consequence).
如果我返回 0.01,那几乎是正确的。但是,当我在模拟器中打开“颜色未对齐图像”时,它会标记所有表格视图单元格(这似乎是一个合乎逻辑的结果)。
The answers to the question UITableView: hide header from empty sectionseem to indicate that some people were successful in hiding the section header. But it might apply to the plain style (instead of the grouped one).
问题UITableView: hide header from empty section的答案似乎表明有些人成功隐藏了部分标题。但它可能适用于普通样式(而不是分组样式)。
The best compromise so far is returning the height 0.5, resulting in a somewhat thicker line below the navigation bar. However, I'd appreciate if somebody knows how the first section header can be completely hidden.
迄今为止最好的折衷方法是将高度返回 0.5,从而在导航栏下方产生一条稍粗的线条。但是,如果有人知道如何完全隐藏第一节标题,我将不胜感激。
Update
更新
According to caglar's analysis (https://stackoverflow.com/a/19056823/413337), the problem only arises if the table view is contained in a navigation controller.
根据caglar的分析(https://stackoverflow.com/a/19056823/413337),只有当表格视图包含在导航控制器中时才会出现问题。
回答by Codo
I have a workaround that seems reasonably clean to me. So I'm answering my own question.
我有一个对我来说似乎相当干净的解决方法。所以我在回答我自己的问题。
Since 0 as the first section header's height doesn't work, I return 1. Then I use the contentInsetto hide that height underneath the navigation bar.
由于 0 作为第一部分标题的高度不起作用,我返回 1。然后我使用contentInset隐藏导航栏下方的高度。
Objective-C:
目标-C:
- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
if (section == 0)
return 1.0f;
return 32.0f;
}
- (NSString*) tableView:(UITableView *) tableView titleForHeaderInSection:(NSInteger)section
{
if (section == 0) {
return nil;
} else {
// return some string here ...
}
}
- (void) viewDidLoad
{
[super viewDidLoad];
self.tableView.contentInset = UIEdgeInsetsMake(-1.0f, 0.0f, 0.0f, 0.0);
}
Swift:
迅速:
override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return section == 0 ? 1.0 : 32
}
override func viewDidLoad() {
super.viewDidLoad()
tableView.contentInset = UIEdgeInsets(top: -1, left: 0, bottom: 0, right: 0)
}
回答by user3378170
This is how to hide the first section header in UITableView (grouped style).
这是如何隐藏 UITableView 中的第一个部分标题(分组样式)。
Swift 3.0 & Xcode 8.0 Solution
Swift 3.0 & Xcode 8.0 解决方案
The TableView's delegate should implement the heightForHeaderInSection method
Within the heightForHeaderInSection method, return the least positive number. (not zero!)
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat { let headerHeight: CGFloat switch section { case 0: // hide the header headerHeight = CGFloat.leastNonzeroMagnitude default: headerHeight = 21 } return headerHeight }
TableView 的委托应该实现 heightForHeaderInSection 方法
在 heightForHeaderInSection 方法中,返回最小的正数。(不是零!)
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat { let headerHeight: CGFloat switch section { case 0: // hide the header headerHeight = CGFloat.leastNonzeroMagnitude default: headerHeight = 21 } return headerHeight }
回答by Giorgos Ath
The answer was very funny for me and my team, and worked like a charm
答案对我和我的团队来说非常有趣,而且效果很好
- In the Interface Builder, Just move the tableview under another view in the view hierarchy.
- 在 Interface Builder 中,只需将 tableview 移动到视图层次结构中的另一个视图下。
REASON:
原因:
We observed that this happens only for the First View in the View Hierarchy, if this first view is a UITableView. So, all other similar UITableViews do not have this annoying section, except the first. We Tried moving the UITableView out of the first place in the view hierarchy, and everything was working as expected.
我们观察到这种情况只发生在视图层次结构中的第一个视图中,如果第一个视图是 UITableView。所以,除了第一个之外,所有其他类似的 UITableViews 都没有这个烦人的部分。我们尝试将 UITableView 从视图层次结构中的第一个位置移出,一切都按预期工作。
回答by Nitesh Borad
Use this trick for grouped type tableView
将此技巧用于分组类型 tableView
Copy paste below code for your table view in viewDidLoadmethod:
在viewDidLoad方法中为您的表视图复制粘贴以下代码:
tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, tableView.bounds.size.width, 0.01f)];
回答by TonnyTao
this way is OK.
这样就可以了。
override func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
if section == 0 {
return CGFloat.min
}
return 25
}
override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
if section == 0 {
return nil
}else {
...
}
}
回答by caglar
I just copied your code and tried. It runs normally (tried in simulator). I attached result view. You want such view, right? Or I misunderstood your problem?
我刚刚复制了您的代码并尝试了。它运行正常(在模拟器中尝试过)。我附上了结果视图。你想要这样的景色,对吧?还是我误解了你的问题?
回答by h.martin
Swift3 : heightForHeaderInSection works with 0, you just have to make sure header is set to clipsToBounds.
Swift3 :heightForHeaderInSection 与 0 一起使用,您只需要确保将 header 设置为 clipsToBounds。
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 0
}
if you don't set clipsToBounds hidden header will be visible when scrolling.
如果您不设置 clipsToBounds,则滚动时隐藏的标题将可见。
func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
guard let header = view as? UITableViewHeaderFooterView else { return }
header.clipsToBounds = true
}
回答by Manindra Moharana
Update[9/19/17]: Old answer doesn't work for me anymore in iOS 11. Thanks Apple. The following did:
更新 [9/19/17]:旧答案在 iOS 11 中不再适用于我。谢谢 Apple。做了以下事情:
self.tableView.sectionHeaderHeight = UITableViewAutomaticDimension;
self.tableView.estimatedSectionHeaderHeight = 20.0f;
self.tableView.contentInset = UIEdgeInsetsMake(-18.0, 0.0f, 0.0f, 0.0);
Previous Answer:
上一个答案:
As posted in the comments by Chris Ostomothe following worked for me:
正如Chris Ostomo在评论中发布的那样,以下内容对我有用:
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return CGFLOAT_MIN; // to get rid of empty section header
}
回答by Harris
Here is how to get rid of the top section header in a grouped UITableView, in Swift:
以下是如何在 Swift 中删除分组 UITableView 中的顶部部分标题:
tableView.tableHeaderView = UIView(frame: CGRect(x: 0, y: 0, width: 0, height: CGFloat.leastNormalMagnitude))
回答by JaneGoodall
In Swift 4.2 and many earlier versions, instead of setting the first header's height to 0 like in the other answers, you can just set the other headers to nil
. Say you have two sections and only want the second one (i.e., 1
) to have a header. That header will have the text Foobar:
在 Swift 4.2 和许多早期版本中,您无需像其他答案那样将第一个标题的高度设置为 0,而只需将其他标题设置为nil
. 假设您有两个部分并且只希望第二个部分(即1
)有一个标题。该标题将包含文本Foobar:
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return section == 1 ? "Foobar" : nil
}