2011-06-23 2 views
2

EDIT: これを何度も乱してしまえば、私の持つ本当の疑問は次のようになります。 1. UITableViewはビュー全体を占有していますか?
2.そうであれば、どのようにしてセルの境界がビューの一部を占めるかのように設定されます。 3.どのようにして細胞の境界を取得するか - より正確には、細胞が占有している可視領域の境界をどのように知ることができますか?ビューの幅を返すので、self.tableView.bounds.size.widthは役に立ちません。おかげさまで 私の質問をより明確にするために前の情報を残しておきます。オリエンテーションをサポートするUITableViewセクションフッタを中央に配置する方法は?

これは可能ですか? 私はリンゴのドキュメントを読んで、ここや他の場所でフォーラムを回りましたが、これを見つけたり答えたりすることはできません。 UITableVIew内のフッターは実際に何をしても全体のビューを占めますか?テーブル幅の概念はありませんか?

例:

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section { 
    UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 10)]; 
    [footerView setBackgroundColor:[UIColor redColor]]; 

    return footerView; 
} 

このコードは、他の1つの縁部から赤い線を作成します。どのような境界線を指定しても、線は全体のビューを占有します。この問題は、フッターのラベルを中央に配置したい場合、向きの変更をサポートしている場合に、中心がどこにあるかを知る方法がないことです。 は、例えば、私は次の操作を実行しようとしているiPadアプリに:

if ([footerText length] > 0) { 
    UIView *customView = [[[UIView alloc]initWithFrame:CGRectMake(0.0, 0.0, 0, 0.0)] autorelease]; 

    [customView setBackgroundColor:[UIColor grayColor]]; 

    UILabel *footerLabel = [[UILabel alloc] initWithFrame:CGRectZero]; 
    footerLabel.numberOfLines = 2; 
    footerLabel.adjustsFontSizeToFitWidth = NO; 
    [footerLabel setTextAlignment:UITextAlignmentCenter]; 
    [footerLabel setBackgroundColor:[UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:0.5]]; 
    [footerLabel setOpaque:NO]; 
    [footerLabel setTextColor:[UIColor whiteColor]]; 
    [footerLabel setShadowColor:[UIColor lightGrayColor]]; 
    [footerLabel setFont:[UIFont boldSystemFontOfSize:14]]; 
    [footerLabel setFrame:CGRectMake(customView.center.x/0.3, 0.0, 600, 40.0)]; 
    [footerLabel setText:footerText]; 
    [customView addSubview:footerLabel]; 
    [footerLabel release]; 

    NSLog(@"customView width = %f", customView.frame.size.width); 
    NSLog(@"tableview width = %f", self.tableView.frame.size.width); 
    NSLog(@"tableview center = %f", self.tableView.center.x); 

    return customView; 
} else { 
    return nil; 
} 

肖像画で、テーブルの中央には、384(それは詳細ビュー/右側にあります)と風景の中に351.5でなければなりません。しかし、私がsetCenterを使うか、その中心に基づいて左端を調整しようとすると、中心にはなりません。

最終的な質問:フッターにテーブル境界の概念がないように見える場合、どのように向きをサポートしてフッターにカスタムビューを配置するのですか?これは誰かによって解決された問題でなければならないが、私はそれを見つけることができないので、私はドキュメントで何かを見逃しているにちがいない。 お時間をいただきありがとうございます。

答えて

7

テーブルビュー内に何かを配置するには、コンテナにラップし、埋め込みビューとコンテナの両方に対して適切な自動サイズ調整マスクを設定する必要があります。

コンテナの幅は自由に設定でき、埋め込みビューには両方の柔軟なサイドマージンが必要です。

例:

- (UIView *) tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section 
{ 
    static UIView *footerView; 

    if (footerView != nil) 
     return footerView; 

    NSString *footerText = NSLocalizedString(@"Some Display Text Key", 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 footerWidth = 150.0f; 
    float padding = 10.0f; // an arbitrary amount to center the label in the container 

    footerView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, footerWidth, 44.0f)]; 
    footerView.autoresizingMask = UIViewAutoresizingFlexibleWidth; 

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

    [footerView addSubview:footerLabel]; 

    return footerView; 
} 
+0

呪い! UIViewAutoresizingFlexibleWidthが問題でした!それは私が見たはずの何かであるときにそれを嫌う。ありがとうcodelark! – addzo

+0

テキストをセンタリングするのに –

関連する問題