cellForRowAtIndexPath
で現在の向きを確認し、セルを正しく設定する必要があります。 IBで2つの異なるセルを作成することができます。
また、回転イベント(shouldAutorotateToInterfaceOrientation
またはdidRotateFromInterfaceOrientation
)のコールバックの1つに[myTableView reloadData]
を呼び出す必要があります。 cellForRowAtIndexPath
は、[myTableView reloadData]
(すべてのセルに対して)を呼び出すたびに呼び出されます。 セルを再利用するために異なる識別子を使用してください。
EDIT:これは私がこれをコーディングする方法を次のとおりです。多分何か、各セルの識別子プロパティを設定し、Interface Builderで
IBOutlet MyCustomCell1 * customCell1;
IBOutlet MyCustomCell2 * customCell2;
:
あなたの.hファイルに2 IBOutletsを追加します。 cellIdentifier1
とcellIdentifier2
のようになります。 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;
}
}
私は非常に識別子に精通していません:)彼らは何ですか?私はそれらを知らないでいくつかの単純なリストを出しました... – Centurion
セルレンダリングプロセスをスピードアップするために、[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
ありがとう、私はアイデアを持って:)今、あなたはより詳細に異なる識別子を設定する方法について説明することができますか?私はcellForRowAtIndexPathからdequeueReusableCellWithIdentifierへの2つのseparete呼び出しを持っているはずですか?つまり、現在の向きを調べる必要があります(mode = portrait)。その後、potrait-mode-identifierを渡すセルをデキューします。そうでなければlandscape-mode-identifierを使用してデキューしますか?私はcellForRowAtIndexPathメソッドtotalyを書き直さなければならないようです。 – Centurion