XIBファイルを使用してカスタムUITableViewCellを作成し、UITableViewで機能させました。カスタムセルとそのサブビューを縦向きのサイズで設計しましたが、デバイスをランドスケープモードで回転させると、セルとそのサブビューのサイズを変更したいと思います。このために次のコードを記述しましたが、表示されているセルの1つのセルだけがサイズ変更され、残りは同じままである理由がわかりません。誰かがカスタムセルとそのサブビューのサイズを変更するための標準的な方法を手伝ってくれませんか?ありがとう。
#pragma mark - UITableView delegate methods
- (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"CustomTableViewCellIdentifier";
CustomTableViewCell *cell = (CustomTableViewCell*)[tv dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
// Load custom cell from NIB
self.customTableViewCell = [[[NSBundle mainBundle] loadNibNamed:@"CustomTableViewCell" owner:self options:nil] objectAtIndex:0];
cell = _customTableViewCell;
}
// Use cell
...
}
#pragma mark - Layout views
- (void)layoutForOrientation:(UIInterfaceOrientation)orientation {
if (UIInterfaceOrientationIsPortrait(orientation))
[self layoutPortraitViews];
else
[self layoutLandscapViews];
}
- (void)layoutLandscapViews {
self.customTableViewCell.frame = CGRectMake(0, 0, 1024, 40);
self.customTableViewCell.contentSize = CGSizeMake(1024, 40);
self.customTableViewCell.cellBackgroundImageView.frame = CGRectMake(0, 0, 1024, 40);
self.customTableViewCell.assetNameLabel.frame = CGRectMake(15, 0, 245, 20);
self.customTableViewCell.assetTypeLabel.frame = CGRectMake(15, 20, 245, 20);
self.customTableViewCell.assetImageView.frame = CGRectMake(265, 20, 120, 20);
self.customTableViewCell.assetValueLabel.frame = CGRectMake(265, 20, 120, 20);
[self.tableView reloadData];
}
- (void)layoutPortraitViews {
self.customTableViewCell.frame = CGRectMake(0, 0, 768, 40);
self.customTableViewCell.contentSize = CGSizeMake(768, 40);
self.customTableViewCell.cellBackgroundImageView.frame = CGRectMake(0, 0, 768, 40);
self.customTableViewCell.assetNameLabel.frame = CGRectMake(15, 0, 180, 20);
self.customTableViewCell.assetTypeLabel.frame = CGRectMake(15, 20, 180, 20);
self.customTableViewCell.assetImageView.frame = CGRectMake(200, 20, 90, 20);
self.customTableViewCell.assetValueLabel.frame = CGRectMake(200, 20, 90, 20);
[self.tableView reloadData];
}
[解決]
少し苦労した後、これが機能しました。セルのサイズを変更すると、方向の変更を処理し、パフォーマンスのためにセルを再利用し続ける必要があるため、サブビューが機能しなくなります。デバイスを回転させると、表示されるセルが変更されるため、最初のアプローチでは解決策を見つけることができませんでした。これが私の別の解決策です。
ここでの課題は、パフォーマンスを損なうことなく、方向とカスタムセルを一緒に処理することです。
NIBで2つのセルビューを作成しました。1つはポートレート用、もう1つはランドスケープ用です。
2つの変数を使用しました。
BOOL refreshing;
UIInterfaceOrientation currentOrientation;
ビューからが表示されます。
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
currentOrientation = [UIApplication sharedApplication].statusBarOrientation;
[self layoutForOrientation:[UIApplication sharedApplication].statusBarOrientation];
}
ローテーションデリゲートから。
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
refreshing = YES;
currentOrientation = toInterfaceOrientation;
[UIView animateWithDuration:duration animations:^{
[self.tableView reloadData];
[self layoutForOrientation:toInterfaceOrientation];
}];
}
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
refreshing = NO;
}
テーブルビューデリゲートから。
- (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"CustomTableViewCellIdentifier";
CustomTableViewCell *cell = (CustomTableViewCell*)[tv dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil || refreshing)
{
if (UIInterfaceOrientationIsPortrait(currentOrientation))
cell = [[[NSBundle mainBundle] loadNibNamed:@"CustomTableViewCell" owner:self options:nil] objectAtIndex:0];
else
cell = [[[NSBundle mainBundle] loadNibNamed:@"CustomTableViewCell" owner:self options:nil] objectAtIndex:1];
}
//Use cell
}
...したがって、このソリューションは、方向変更イベントが発生した場合にのみセルをリロードします。ハッピーコーディング:-)