2

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
}

...したがって、このソリューションは、方向変更イベントが発生した場合にのみセルをリロードします。ハッピーコーディング:-)

4

3 に答える 3

0

カスタムNIBxibセルのサイズを変更すると、回転時に必要なUIが提供される場合と提供されない場合があります。タスクを簡単にするために、2つのUITableViewCellペン先を作成します。

 if (!cell) {
            if(UIInterfaceOrientationIsLandscape(self.interfaceOrientation)) {
                [[NSBundle mainBundle] loadNibNamed:@"CustomTableViewCell-Landscape" owner:self options:nil];
            }else {
                [[NSBundle mainBundle] loadNibNamed:@"CustomTableViewCell" owner:self options:nil];
     }
}

[tableView reloadData];向きの変更をさらに求める

于 2012-11-02T11:56:27.930 に答える
0

いつでもself.customTableViewCell瞬時に上書きするからです。したがって、最後のオブジェクトのみを返します。

すべてのサイズを変更するには、以下のコードを記述する必要がありますUITableViewCells

for(UITableViewCell *cell in self.tableView.visibleCells)
{
     if([cell isKindOfClass:[UITableViewCell class]])
     {
          cell.frame = CGRectMake(0, 0, 1024, 40);
          cell.contentSize = CGSizeMake(1024, 40);
          cell.cellBackgroundImageView.frame = CGRectMake(0, 0, 1024, 40);
          cell.assetNameLabel.frame = CGRectMake(15, 0, 245, 20);
          cell.assetTypeLabel.frame = CGRectMake(15, 20, 245, 20);
          cell.assetImageView.frame = CGRectMake(265, 20, 120, 20);
          cell.assetValueLabel.frame = CGRectMake(265, 20, 120, 20);
     }
}
于 2012-11-02T11:58:24.667 に答える
0

これは、毎回コードごとにフレームのサイズを変更するのではなく、Interface Builder(または新しい自動レイアウト)内で自動サイズ変更マスクを正しく使用することで実現できると思います。

于 2012-11-02T12:04:26.957 に答える