在Landscape中自动调整UITableCells的内容

在UITableView,我通过UILabels添加内容到我的单元格。

为了定义最佳尺寸(与单元格宽度所允许的一样大),我注意到只有tableView.contentSize.width是可靠的,因为cell.contentView.bounds总是提供纵向宽度,即使在横向

在与autoresizingMask结合使用时效果autoresizingMask :当我从纵向切换到横向时,再次切换到纵向。

当我直接在景观中加载视图时出现问题。 我的UILabels的宽度比屏幕大,即使断点显示我正确的宽度 tableView.contentSize.width

横向和纵向之间的切换没有改变,宽度仍然大于屏幕。

如果我不使用autoresizingMask,宽度是正确的,如果我使用它,即使是一个短文本,它会离开屏幕(但我注意到它只是由于testing背景颜色或使用非常大的NSString)。

简单来说:

  • 肖像>风景>人像>风景… 很好(完美resize)
  • 风景>人像>风景>人像… 错误(宽度超出范围)

我的代码简化了:

 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { //[...] UIFont *font = [UIFont fontWithName:@"Helvetica-Bold" size:12.0]; CGRect frame = CGRectMake(cell.contentView.bounds.origin.x + 20, cell.contentView.bounds.origin.y + 4, tableView.contentSize.width - 50, font.lineHeight); //at this point a breakpoint shows that frame is whatever I asked (ex: 200 for test) //but still the labels is wider than the screen UILabel *result = [[[UILabel alloc] initWithFrame:frame] autorelease]; [result setText:@"bouh!"]; result.lineBreakMode = UILineBreakModeWordWrap; result.numberOfLines = 1; result.font = font; //if I comment this line, the width is always what I want result.autoresizingMask = UIViewAutoresizingFlexibleWidth; //test to see the real size result.backgroundColor = [UIColor orangeColor]; [cell.contentView addSubview:result]; //[...] return cell; } 

我在这里请求你的帮助,看看有没有更好的方法去做我想要的? 如果没有,我做错了什么?

我找不到一个干净简单的通用解决scheme(适应每个屏幕)。

但是这是我做的工作:

  //in portrait cell width works fine with autoResize //in landscape tableView width works fine with autoResize CGFloat cellWidth; if ( [UIDevice currentDevice].orientation != UIDeviceOrientationLandscapeLeft && [UIDevice currentDevice].orientation != UIDeviceOrientationLandscapeRight) { cellWidth = tableView.contentSize.width; } else { cellWidth = cell.contentView.bounds.size.width; } 

所以,在后面的代码中,我是这么用的:

 CGRect frame = CGRectMake(cell.contentView.bounds.origin.x + 20, cell.contentView.bounds.origin.y + 4, cellWidth - 50, font.lineHeight); UILabel *result = [[[UILabel alloc] initWithFrame:frame] autorelease]; 

但有些奇怪:我需要总是使用tableView.contentSize.width作为我的活动指标的框架,因为cell.contentView.bounds.size.width只有320,即使在景观

 CGRect activityIndicatorRect = CGRectMake( cell.contentView.bounds.origin.x + tableView.contentSize.width - 60 , cell.contentView.bounds.origin.y + 17, 30, 30); 

我倾向于做的是子类UITableViewCell,然后重写initWithReuseIdentifier …

 - (id)initWithReuseIdentifier:(NSString *)reuseIdentifier { self = [super initWithReuseIdentifier:reuseIdentifier]; if (self != nil) { [self setFrame:CGRectMake(0.0, 0.0, kLayoutWidth, kLayoutHeight)]; // Set up labels, buttons etc.... } } 

kLayoutWidth和kLayoutHeight是您可以设置为任意值的定义,您可以将值直接放在CGRect中,但我更喜欢在编译器定义的类的顶部指定大小。

这样做的好处是,现在你可以以固定的大小布局你的单元格,并为内容元素设置自动调整的掩码,当tableview调整单元格的大小时,它将全部resize。

你也可以使用不同的笔尖,用于纵向/横向/ iPhone / iPad,并相应地加载它们。 为您节省大量的代码

 static NSString *aCustomCellIdentifier = @"aCustomCell"; CustomCell *cell = (CustomCell*)[tableView dequeueReusableCellWithIdentifier:aCustomCellIdentifier]; if (!cell) { if (!IS_IPAD) cell = [[[UINib nibWithNibName:@"CustomCell" bundle:nil] instantiateWithOwner:self options:nil] lastObject]; else cell = [[[UINib nibWithNibName:@"CustomCell_iPad" bundle:nil] instantiateWithOwner:self options:nil] lastObject]; }