0

皆さん、こんにちは。

当初は iOS6.1 の iPhone をターゲットにしていますが、iOS5.1 の iPhone でも問題なく動作するヒントとコードを歓迎します。

サブビューとして7つの異なるラベルを持つカスタム UITableViewCell があります:

-(id) initWithStyle: (UITableViewCellStyle) style reuseIdentifier: (NSString*) reuseIdentifier
{
    self = [super initWithStyle : style reuseIdentifier : reuseIdentifier] ;

    if (self){

//Adding Subviews

[self.contentView addSubView : self.myLabel1] ;
[self.contentView addSubView : self.myLabel2] ;
[self.contentView addSubView : self.myLabel3] ;
[self.contentView addSubView : self.myLabel4] ;
[self.contentView addSubView : self.myLabel5] ;
[self.contentView addSubView : self.myLabel6] ;
[self.contentView addSubView : self.myLabel7] ;
}
return self;
}

ラベルを遅延インスタンス化するとき、開始方向は縦向きで、ラベルは 2 つの異なる行に配置されていると思います。

-(UILabel*) myLabel1
{
    if (!_myLabel1){
        _myLabel1 = [UILabel alloc] initWithFrame: CGRectMake(10,30,40,20);
        _myLabel1.numberOfLines = 1;
        _myLabel1.opaque = YES;
}
return _myLabel1; 
}

-(UILabel*) myLabel2
{
    if (!_myLabel2){
        _myLabel2 = [UILabel alloc] initWithFrame: CGRectMake(50,30,40,20);
        _myLabel2.numberOfLines = 1;
        _myLabel2.opaque = YES;
}
return _myLabel2; 
}

-(UILabel*) myLabel3
{
    if (!_myLabel3){
        _myLabel3 = [UILabel alloc] initWithFrame: CGRectMake(90,30,40,20);
        _myLabel3.numberOfLines = 1;
        _myLabel3.opaque = YES;
}
return _myLabel3; 
}


-(UILabel*) myLabel4
{
    if (!_myLabel4){
        _myLabel4 = [UILabel alloc] initWithFrame: CGRectMake(10,60,40,20);
        _myLabel4.numberOfLines = 1;
        _myLabel4.opaque = YES;
}
return _myLabel4; 
}

-(UILabel*) myLabel5
{
    if (!_myLabel5){
        _myLabel5 = [UILabel alloc] initWithFrame: CGRectMake(50,60,40,20);
        _myLabel5.numberOfLines = 1;
        _myLabel5.opaque = YES;
}
return _myLabel5; 
}


-(UILabel*) myLabel6
{
    if (!_myLabel6){
        _myLabel6 = [UILabel alloc] initWithFrame: CGRectMake(90,60,40,20);
        _myLabel6.numberOfLines = 1;
        _myLabel6.opaque = YES;
}
return _myLabel6; 
}


-(UILabel*) myLabel7
{
    if (!_myLabel7){
        _myLabel7 = [UILabel alloc] initWithFrame: CGRectMake(130,60,40,20);
        _myLabel7.numberOfLines = 1;
        _myLabel7.opaque = YES;
}
return _myLabel7; 
}

これを達成するための最良の方法は何ですか?

「縦向きの場合は、上記のようにラベルを 2 つの異なる行に配置します。向きが横向きの場合は、すべてのラベルが 1 行に表示されるように配置します。ユーザーが向きを変更した場合、再配置は自動的に行われます。

ランドスケープ モードでの再配置では、すべてのラベルが 30 の y 値に揃えられる必要があります (最初の行が 30、2 行目が 60 ではなく)。ラベル 4、5、6、7 の x 値も変更する必要があります。これは、ラベル 1、2、3 の下ではなく、ラベル 3 の右側に配置されるためです。」</p>

ところで、実際の iPhone でアプリを起動すると、机の上に「縦」のままにすると、iOS 5.1 と iIOS 6.1 の両方で向きが誤って「横」と報告されることがあることがわかりました。これを見つけたのは、layoutSubviews をいじり始めたからです: デバイスの向きに応じてビューごとに新しいフレームを作成するビューを再配置するのに最適な場所だと思ったからです。これは自動レイアウトの最良のケースですか? ビューはプログラムで作成されます。

ありがとう

ニコラ

4

1 に答える 1

0

答えてくれてありがとうカルペッシュですが、実際にはさらに試行錯誤した結果、それを処理する最良の方法は、UITableViewCellにコントローラーへのポインターを配置し、それを使用して新しい向きを取得し、layoutSubviewsで再配置を行うことであることがわかりました:

@property(weak,nonatomic) UITableViewController *controller;

-(void) layoutSubviews
{
    [super layoutSubviews];


    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone){

        // I am on a Phone

        NSLog(@"The new controller orientation is: %u", self.controller.interfaceOrientation);

        if(self.controller.interfaceOrientation == UIInterfaceOrientationPortrait){
            NSLog(@"Phone - Controller orientation: Portrait.");

            //Layout the subviews for portrait orientation

            self.myLabel1.frame = CGRectMake(10,70,(self.frame.size.width-20)/4,20);

            //..

        } else{
            NSLog(@"Phone - Controller orientation: Landscape.");

            //Layout the subviews for landscape orientation

            self.myLabel1.frame = CGRectMake(10,30,(self.frame.size.width-20)/4,20);

            //..
        }

    } else if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad){

        // I am on a Pad

        if(self.controller.interfaceOrientation == UIInterfaceOrientationPortrait){
            NSLog(@"Pad - Controller orientation: Portrait.");

            //Layout the subviews for portrait orientation

        } else{
            NSLog(@"Pad - Controller orientation: Landscape.");

            //Layout the subviews for landscape orientation
        }

    }
}

とにかく、あなたが提案したテーブルの更新は、8 つの行を変更するのに役立ちます!

編集:向きが変わるとセルの高さが自動的に再読み込みされるため、テーブルを強制的に再読み込みする必要はありません。ニコラ

于 2013-02-12T19:55:13.677 に答える