如何隐藏 UITableView 中的第一节标题(分组样式) [英] How to hide first section header in UITableView (grouped style)

查看:32
本文介绍了如何隐藏 UITableView 中的第一节标题(分组样式)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于使用分组样式的表格视图的设计在 iOS 7 中发生了很大变化,我想隐藏(或删除)第一节标题.到目前为止,我还没有实现.

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.

稍微简化了一点,我的代码是这样的:

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 ...
    }
}

如果我返回高度为 0,则其他两个方法将永远不会以部分索引 0 被调用.但仍然会以默认高度绘制一个空的部分标题.(在 iOS 6 中,这两个方法被调用.但是,可见的结果是一样的.)

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.)

如果我返回不同的值,部分标题将获得指定的高度.

If I return a different value, the section header gets the specified height.

如果我返回 0.01,那几乎是正确的.但是,当我在模拟器中打开Color Misaligned Images"时,它会标记所有表格视图单元格(这似乎是一个合乎逻辑的结果).

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).

问题的答案UITableView:从空部分隐藏标题似乎表示某些人成功隐藏了部分标题.但它可能适用于普通样式(而不是分组样式).

The answers to the question UITableView: hide header from empty section seem 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).

目前最好的折衷方案是将高度返回 0.5,从而在导航栏下方产生一条稍粗的线条.但是,如果有人知道如何完全隐藏第一节标题,我将不胜感激.

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.

更新

根据 caglar 的分析(https://stackoverflow.com/a/19056823/413337),只有当表格视图包含在导航控制器中时才会出现问题.

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.

推荐答案

我有一个在我看来相当干净的解决方法.所以我在回答我自己的问题.

I have a workaround that seems reasonably clean to me. So I'm answering my own question.

由于 0 作为第一个部分标题的高度不起作用,我返回 1.然后我使用 contentInset 隐藏导航栏下方的高度.

Since 0 as the first section header's height doesn't work, I return 1. Then I use the contentInset to hide that height underneath the navigation bar.

目标 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);
}

斯威夫特:

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)
}

这篇关于如何隐藏 UITableView 中的第一节标题(分组样式)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆