2011-05-12 142 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