12

セクションのヘッダーがデフォルトの左ではなく右にある必要があるアプリに取り組んでいます。

私が検索したところ、多くのオタクが実装を提案しました:

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    static UIView *headerView;

    if (headerView != nil)
        return headerView;

    NSString *headerText = NSLocalizedString(@"البحث الأخيرة", nil);

    // set the container width to a known value so that we can center a label in it
    // it will get resized by the tableview since we set autoresizeflags
    float headerWidth = 150.0f;
    float padding = 10.0f; // an arbitrary amount to center the label in the container

    headerView = [[UIView alloc] initWithFrame:CGRectMake(300, 0.0f, headerWidth, 44.0f)];
    headerView.autoresizingMask = UIViewAutoresizingFlexibleWidth;

    // create the label centered in the container, then set the appropriate autoresize mask
    UILabel *headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(padding, 0, headerWidth - 2.0f * padding, 44.0f)];
    headerLabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
    headerLabel.textAlignment = UITextAlignmentRight;
    headerLabel.text = headerText;

    [headerView addSubview:headerLabel];

    return headerView;
}

ただし、ヘッダー ビュー フレームの x 座標を大きくしようとしても、ビューの位置には影響しません。そして、常にテーブル ビューの中央にあります。

ヘッダーが右側にある必要があります。

4

4 に答える 4

15

これは私にとってはうまくいきましたが、必要に応じてフレームの座標を調整してください

-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UILabel *label = [[UILabel alloc] init];
    label.text=@"header title";
    label.backgroundColor=[UIColor clearColor];
    label.textAlignment = NSTextAlignmentRight;
    return label;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 50;
}
于 2012-10-01T11:28:52.577 に答える
0

状況を理解しているかどうかはわかりませんが、ラベルを左ではなく右に揃えたいだけだと思います。

ヘッダー ビューのフレーム値を調整しても機能しません。これは、tableView がフレームの配置を担当し、ここで設定した値を無視するためです。

唯一のオプションは、headerView のサブビューを操作することです。

例、headerLabel の位置を右揃えに設定:

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    static UIView *headerView;

    if (headerView != nil)
        return headerView;

    NSString *headerText = NSLocalizedString(@"البحث الأخيرة", nil);

    float headerWidth = 320.0f;
    float padding = 10.0f;

    headerView = [[UIView alloc] initWithFrame:CGRectMake(300, 0.0f, headerWidth, 44.0f)];
    headerView.autoresizesSubviews = YES;

    // create the label centered in the container, then set the appropriate autoresize mask
    UILabel *headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, headerWidth, 44.0f)];
    headerLabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleWidth;
    headerLabel.textAlignment = UITextAlignmentRight;
    headerLabel.text = headerText;

    CGSize textSize = [headerText sizeWithFont:headerLabel.font constrainedToSize:headerLabel.frame.size lineBreakMode:UILineBreakModeWordWrap];
    headerLabel.frame = CGRectMake(headerWidth - (textSize.width + padding), headerLabel.frame.origin.y, textSize.width, headerLabel.frame.size.height);

    [headerView addSubview:headerLabel];

    return headerView;
}
于 2012-10-01T11:30:21.570 に答える
0

まず第一に、私はuiviewを初期化しません

 headerView = [[UIView alloc] initWithFrame:CGRectMake(300, 0.0f, headerWidth, 44.0f)];

原点の x 座標は 300 になるため、ヘッダー ビューとフッター ビューの場合、原点座標を CGPoint (0.0) に設定し、サイズを調整します。

ラベルをビュー自体と同じ大きさにして、配置を右に設定してみてください。

UIView *headerTitleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 40)];

UILabel *sectionTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, headerTitleView.frame.size.width, 40)];
sectionTitleLabel.backgroundColor = [UIColor clearColor];
sectionTitleLabel.textAlignment = UITextAlignmentright;

もう 1 つの可能性は、ヘッダー ビューの右側のどこかに UIlabel を中央揃えで追加することです。

于 2012-10-01T11:26:06.160 に答える