2011-09-10 12 views
0

私が現在UITableViewControllerでやりたいことは、ポートレートモードでは1列セル行、横長モードでは2列セル行です。表示の便宜のために(セルを表示するために使用可能な幅のスペースを使用する)ため、両方の列セルは同じ形式です。しかし、私はそれを実装する方法がわかりません。回転時にUITableViewControllerの外観を変更するにはどうすればよいですか?

だから、 "cellForRowAtIndexPath"メソッドで自分のセルのカスタマイズを行い、現在の画面モードを確認することが考えられます。質問は、 "shouldAutorotateToInterfaceOrientation"でフラグを設定する必要があるのですか、それともいくつかの設定がありますか?

第2に、テーブルのセルを再描画するには、「shouldAutorotateToInterfaceOrientation」でテーブルの再読み込みを呼び出すだけで十分ですか?

また、私は別のペン先を作ってIBで自分の細胞をデザインすることを考えています。私はそれが別の問題だと思うが、それがどのようにソリューションに影響するのだろうと思っている。

答えて

1

cellForRowAtIndexPathで現在の向きを確認し、セルを正しく設定する必要があります。 IBで2つの異なるセルを作成することができます。

また、回転イベント(shouldAutorotateToInterfaceOrientationまたはdidRotateFromInterfaceOrientation)のコールバックの1つに[myTableView reloadData]を呼び出す必要があります。 cellForRowAtIndexPathは、[myTableView reloadData](すべてのセルに対して)を呼び出すたびに呼び出されます。 セルを再利用するために異なる識別子を使用してください。

EDIT:これは私がこれをコーディングする方法を次のとおりです。多分何か、各セルの識別子プロパティを設定し、Interface Builderで

IBOutlet MyCustomCell1 * customCell1; 
IBOutlet MyCustomCell2 * customCell2; 

あなたの.hファイルに2 IBOutletsを追加します。 cellIdentifier1cellIdentifier2のようになります。 IBのファイル所有者がdataSource(cellForRowAtIndexPathが実装されている場所)であることを確認してください。

cellForRowAtIndexPathは次のようになります返すようにしてください、

また
if (self.interfaceOrientation == UIInterfaceOrientationPortrait || 
    self.interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) { 
    // use a portrait cell 
} else { 
    // use a landscape cell 
} 

YES:あなたのコードで

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    if ([UIDevice currentDevice].orientation == UIDeviceOrientationLandscapeLeft 
    || [UIDevice currentDevice].orientation == UIDeviceOrientationLandscaperight) 
    { 
     //Landscape, lets use MyCustomCell2. 
     NSString * cellIdentifier2 = @"cellIdentifier2"; 

     MyCustomCell2 * cell = (MyCustomCell2 *) [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 

     if (cell == nil) { 
     //We have to initialize the cell, we're going to use IB 
     [[NSBundle mainBundle] loadNibNamed:@"CustomCell2NibName" owner:self options:nil]; 
     //After this, customCell2 we defined in .h is initialized from IB 
     cell = customCell2; 

     } 
     //setup the cell, set text and everything. 

     return cell; 
    } 

    else 
    { 
    //portrait case, the same as before but using CustomCell1 
    NSString * cellIdentifier1 = @"cellIdentifier1"; 

     MyCustomCell1 * cell = (MyCustomCell1 *) [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 

     if (cell == nil) { 
     //We have to initialize the cell, we're going to use IB 
     [[NSBundle mainBundle] loadNibNamed:@"CustomCell1NibName" owner:self options:nil]; 
     //After this, customCell1 we defined in .h is initialized from IB 
     cell = customCell1; 

     } 
     //setup the cell, set text and everything. 

     return cell; 


    } 

}

+0

私は非常に識別子に精通していません:)彼らは何ですか?私はそれらを知らないでいくつかの単純なリストを出しました... – Centurion

+0

セルレンダリングプロセスをスピードアップするために、[yourTable dequeueReusableCellWithIdentifier]を使用してセルを作成することができます。そのセレクタは、UITableviewCellを返します。もしそれがnilなら、初めてセルが作成され、それを初期化しています。 [このチュートリアル](http://www.iphonedeveloperdiary.com/2010/03/correct-use-of-dequeuereusablecellwithidentifier-in-a-table-view-on-the-iphone/)をチェックするか、iOSリファレンスガイドをご覧ください。 more info;) – ender

+0

ありがとう、私はアイデアを持って:)今、あなたはより詳細に異なる識別子を設定する方法について説明することができますか?私はcellForRowAtIndexPathからdequeueReusableCellWithIdentifierへの2つのseparete呼び出しを持っているはずですか?つまり、現在の向きを調べる必要があります(mode = portrait)。その後、potrait-mode-identifierを渡すセルをデキューします。そうでなければlandscape-mode-identifierを使用してデキューしますか?私はcellForRowAtIndexPathメソッドtotalyを書き直さなければならないようです。 – Centurion

1

tableView:cellForRowAtIndexPath:ため、あなたが現在の向きを確認することができますshouldAutorotateToInterfaceOrientation:。ローテーション(didRotateFromInterfaceOrientation:)後にtableView[tableView reloadData];に再ロードして、正しいセルが使用されていることを確認する必要があります。

関連する問題