2011-05-12 156 views
0

在我的UITableViewCells中,我显示了不同长度的文本。为了容纳更大数量的文本,并同时不要求小文达在巨大的表格单元格,我在这里设置行的高度...UITableViewCells的不同高度的标签改变标签的宽度

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    float padding = 40.0f; 
    CGSize constraintSize; 
    constraintSize.width = 320.0f - padding - padding; 
    constraintSize.height = MAXFLOAT; 
    CGSize theSize = [thisrowstext sizeWithFont:[UIFont systemFontOfSize:14.0f] 
          constrainedToSize:constraintSize 
           lineBreakMode:UILineBreakModeWordWrap]; 

    if(theSize.height < 24.0f) return 44.0f; 
    else return theSize.height + 20.0f; 
} 

...它工作得很好,不幸的是,为textLabel的宽度似乎也受到一些文本标志(取决于行数)在几个像素中的影响。我尝试过设置缩进值,但这不起作用。有人遇到过这种情况么?

编辑:我添加了我使用的UITableViewCell子类的layoutSubviews方法(没有NIB)。

- (void)layoutSubviews 
{ 
    [super layoutSubviews]; 

    if (self.hideImage) 
    { 
      self.imageView.alpha = 0.0f; 
      self.imageView.frame = CGRectMake(-40.0f, 1.0f, 40.0f, 40.0f); 
      CGRect frame = self.textLabel.frame; 
      self.textLabel.frame = CGRectMake(frame.origin.x - 40.0f, frame.origin.y, frame.size.width + 40.0f, frame.size.height); 
      [self.textLabel setNeedsLayout]; 

    } 
    else 
    { 
      self.imageView.alpha = 1.0f; 
      self.imageView.frame = CGRectMake(1.0f, 1.0f, 40.0f, 40.0f);   
      [self.textLabel setNeedsLayout]; 
    } 
} 

编辑:另外增加的cellForRowAtIndexPath

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *TableCellViewWithHidableImageIdentifier = @"TableCellViewWithHidableImage"; 
    TableCellViewWithHidableImage *cell = (TableCellViewWithHidableImage *)[tableView dequeueReusableCellWithIdentifier:TableCellViewWithHidableImageIdentifier]; 
    if (cell == nil) 
    { 
     cell = [[[TableCellViewWithHidableImage alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:TableCellViewWithHidableImageIdentifier] autorelease]; 
    } 

    cell.hideImage = NO; 
    cell.imageView.image = [UIImage imageNamed:@"empty.png"]; 
    cell.textLabel.font = [UIFont systemFontOfSize:16.0f]; 
    cell.textLabel.numberOfLines = 0; 
    cell.textLabel.text = @"whatever"; 
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 
    return cell;  
} 

enter image description here

回答

0

这是一个猜测,但它在我看来像它是单元格左侧的imageView。看起来像细胞越来越高,它变得越来越宽(试图保持高宽比?),这是推动你的文本向右。奇怪的是你的形象没有延伸。在layoutSubviews期间查看图像视图的情况值得期待。如果你的自定义单元没有实现这个方法,也许基类的实现正在做你不期望的事情。您可以覆盖它并在调用[super layoutSubviews]前后查看图像视图的框架,以查看发生了什么。

+0

良好的通话,让我玩这个,谢谢。 – rob5408 2011-05-14 23:27:52

+0

我将单元格的背景设置为灰色以更好地查看发生了什么。仔细查看单元格的布局这些子目录让我得出了答案。 textLabel缩小了框架的宽度,然后“漂浮”到正确的位置。我无法弄清楚在单元格上改变什么属性使其“左”浮动,因此我手动将框架的原点x设置为0。 – rob5408 2011-05-15 21:41:16

1

很难说没有看到你是如何生成的表格单元格。你用细胞为你的细胞?我发现使用自定义表格单元的笔尖要容易得多,所以如果您还没有使用它,请尝试一下。

我怀疑你的autoresizeMask在一个或多个表格单元格的子视图上可能有问题。

+0

这可能会有所帮助,请参阅上文。谢谢! Rob – rob5408 2011-05-12 20:45:45