调整自定义UITableViewCell及其子视图的大小

时间:2012-11-02 11:43:46

标签: ios uitableview resize subviews

我使用XIB文件创建了Custom UITableViewCell,并使其在UITableView中运行。我设计了自定义单元格及其纵向尺寸的子视图,但是当我在景观模式下旋转设备时,我想调整单元格及其子视图的大小。我已经为此编写了以下代码,但不确定为什么只有一个单元格在可见单元格中调整大小并且其余部分保持不变!有人可以帮我调整标准做法来调整自定义单元格及其子视图的大小吗?感谢。

#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中创建了两个单元格视图,一个用于纵向,另一个用于景观。

使用了两个变量;

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;
}

来自tableview委托;

- (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
}

...所以这个解决方案只有在方向改变事件发生时才会重新加载单元格。快乐的编码: - )

3 个答案:

答案 0 :(得分:0)

调整自定义NIB xib单元格的大小可能会也可能不会在旋转时提供欲望的UI。为了使任务更容易..创建两个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];

答案 1 :(得分: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);
     }
}

答案 2 :(得分:0)

我认为这可以通过在界面构建器(或新的自动布局)中正确使用自动调整遮罩来实现,而不是每次都按代码调整帧的大小。

相关问题