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

查看:84
本文介绍了如何隐藏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,那几乎是正确的。但是,当我在模拟器中打开颜色未对齐图像时,它会标记所有表格视图单元格(这似乎是一个合乎逻辑的结果)。

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.

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

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

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