質問

UITableViewの最初のヘッダーの高さを設定したい。他のヘッダーについては、デフォルトの高さのままにしておきます。 " someDefaultHeight"の代わりに使用できる値/定数以下のコードで?

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0)
        return kFirstHeaderHeight;

    return someDefaultHeight;
}

ありがとう

役に立ちましたか?

解決

IOS 5.0以降では、ほとんどのデリゲートメソッドでUITableViewAutomaticDimensionを返すことができます。ドキュメントページの下部

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if(section == CUSTOM_SECTION)
    {
        return CUSTOM_VALUE;
    }
    return UITableViewAutomaticDimension;
}

他のヒント

アプリのデフォルトをチェックすると、グループ化されたテーブルのデフォルトは22の高さであり、グループ化されていないテーブルのデフォルトは10の高さです。

TableViewのセクションsectionHeaderHeightのプロパティの値を確認すると、そのことがわかります。

実際にトリックを行います:)

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if(section == 0)
        return kFirstSectionHeaderHeight;
    return [self sectionHeaderHeight];
}

完全を期すため:iOS7以降では、グループ化されたスタイルセクションヘッダーの高さは、最初のヘッダーが 55.5 で、後続のヘッダーが 38 です。 (DCIntrospectで測定)

ここで正しい答えが何であるかわかりませんが、iOS 5のグループ化されたテーブルビューでは10も22も正しい高さではないようです。これの質問で、少なくともおおよそ正しい高さに見えます。

デフォルトの高さを取得するには、 super で処理します:

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0)
        return kFirstHeaderHeight;

    return [super tableView:tableView heightForHeaderInSection:section];
}

swift 4.2では、UITableView.automaticDimensionを返す必要があります

これでうまくいくはずです

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.section == CUSTOM_SECTION)
    {
        return CUSTOM_VALUE;
    }
    return [tableView rowHeight];
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top