2012-10-01 10 views
12

セクションのヘッダーが既定の左側の代わりに表示される必要があるアプリケーションで作業しています。UITableViewセクションのヘッダーがデフォルトの左側の代わりに右側にあります。

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

- (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; 
} 

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

右にヘッダーが必要です。全ての

+0

代わりflexibleWidthと一緒に行くのは、完全な幅であることを、それを強制することはできませんか?次に、ビューは全幅を消費し、テキストの右端を正当化します。 – bryanmac

+0

あなたは既にほとんど正しい答えを選択しましたが、余白を得るためのスペースを追加することは本当のハックです。あなたの問題を解決する適切な方法は、空のビュー(または背景色を持つビュー)を作成し、それをheaderViewとして使用することです。次に、そのコンテナビューにUILabelを追加し、右側から少しオフセットします。このコンセプトは、空のビューをコンテナとして使用して他のビューを配置することは、一般的で有用なツールです。 –

+0

@DavidHあなたは正しいです。スペースを追加するという考えは良くありません。当分私のために働いていましたが。私は私の質問で言及したように、私はそれが必要とされているところでいつも中心に立っているということを右にしていました。フレームの設定は機能しませんでした。また、ラベルは右揃えでした。私は、UIViewを使用する方が、より多くのカスタマイズオプションを持つ方がはるかに優れていることを理解しています。コメントありがとう! –

答えて

15

、しかし、あなたのニーズに応じて整列するために、フレームの座標と遊ぶ

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

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section 
{ 
    return 50; 
} 
+1

ええ、UILabelがuiviewのサブクラスであることを前提にして正しいです –

+1

ありがとう!出来た :)。しかし、テキストの開始は視界の境界線にほとんど触れていました。私はテキストにいくつかのスペースを追加しました、そして、今、テキストは少し左に向かって詰まっています。 –

+3

'UITextAlignmentRight'は推奨されていません。' NSTextAlignmentRight'を使用する必要があります。 –

0

firt私はあなたの原点X 300.Forヘッダとfooterviewsあなたの原点するCGPoint(0.0)に調整し、ジャストサイズでプレイ設定される座標ので

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

とのUIViewを初期化しないであろう。

ラベルを自分の視野と同じ大きさにし、その位置合わせを右に設定してください。

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; 

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

これは私のために働いた
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; 
} 
+0

アラインメントはすでにrightに設定されています。 –

7

私はこれが私との後縁から適切な間隔での場所のために働いて、トラブル上記の溶液を使用してテキストを変更していましたテーブルビュー。スウィフト

でPSソリューション

UITableViewDataSource

func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat { 
    return 30 
} 

UITAbleViewDelegate

func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? { 
    return "Header Title" 
} 

func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) { 

    let header: UITableViewHeaderFooterView = view as! UITableViewHeaderFooterView 
    header.textLabel?.font = UIFont(name: "AvenirNext-Regular", size: 14.0) 
    header.textLabel?.textAlignment = NSTextAlignment.Right 

} 
関連する問題