4

少なくとも 2 つのラベルを表示するカスタム tableViewHeader を作成する必要があります。projectName と projectDescription。これら 2 つのラベルの内容は (文字列の長さに関して) 異なります。

デフォルトのデバイスの向き (ポートレート) の作業バージョンを取得できましたが、ユーザーがデバイスを横向きに回転すると、カスタム headerView の幅が調整されず、ユーザーは右側に未使用の空白が表示されます。

次のコード フラグメントが使用されています。

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 50.0f;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{    
    CGFloat wd = tableView.bounds.size.width;
    CGFloat ht = tableView.bounds.size.height;
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0., 0., wd, ht)];
    headerView.contentMode = UIViewContentModeScaleToFill;
    // Add project name/description as labels to the headerView.
    UILabel *projName = [[UILabel alloc] initWithFrame:CGRectMake(5., 5., wd, 20)];
    UILabel *projDesc = [[UILabel alloc] initWithFrame:CGRectMake(5., 25., wd, 20)];
    projName.text = @"Project: this project is about an interesting ..";
    projDesc.text = @"Description: a very long description should be more readable when your device is in landscape mode!";
    [headerView addSubview:projName];
    [headerView addSubview:projDesc];
    return headerView;
}

tableView:viewForHeaderInSection:は の後に 1 回だけ呼び出されviewDidLoad、デバイスの向きが変わった後では呼び出されないことに気付きました。

willRotateToInterfaceOrientation:次のような方法でメソッドを実装する必要があります。

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
                                duration:(NSTimeInterval)duration
{
    [self.tableView reloadSections:nil withRowAnimation:UITableViewRowAnimationNone];
}

を機能させる方法がわかりませんreloadSections:。customView の再計算を強制します。

4

1 に答える 1

7

autoresizingMask を使用すると、ヘッダーは回転時に正しく調整されます。他のものをオーバーライドする必要はありません。ビューを返す直前に、コードの最後にオートマスクを設定します。

    [headerView addSubview:projName];
    [headerView addSubview:projDesc];
    projName.autoresizingMask = UIViewAutoresizingFlexibleRightMargin;
    projDesc.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
    headerView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
    return headerView;
于 2012-11-12T01:47:40.040 に答える