2011-05-07 72 views
2

我想根據文本長度調整我在UITableView中的行高度。我有以下代碼:在UITableView中格式化動態行高度掙扎

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSString *cellText =[[topics objectAtIndex:indexPath.row] name]; 
    UIFont *cellFont = [UIFont fontWithName:@"ArialMT" size:17.0]; 
    CGSize constraintSize = CGSizeMake(280.0f, MAXFLOAT); 
    CGSize labelSize = [cellText sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap]; 

    return labelSize.height + 20; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease]; 
     cell.textLabel.lineBreakMode = UILineBreakModeWordWrap; 
     cell.textLabel.numberOfLines = 0; 
     cell.textLabel.font = [UIFont fontWithName:@"ArialMT" size:17.0]; 
    } 
} 

然而,隨着的UIImageView和UIDetailText弄亂了,如下圖所示的圖像:

enter image description here

我該如何解決這個問題?

我已經試過:

[cell.imageView setContentMode:UIViewContentModeScaleToFill]; 
    [cell.imageView setFrame:CGRectMake(0, 0, 16,16)]; 
    [cell.imageView setBounds:CGRectMake(0, 0, 16,16)]; 
    [cell.imageView setAutoresizingMask:UIViewAutoresizingNone]; 
    [cell.imageView setAutoresizesSubviews:NO]; 

,並沒有一個似乎工作

+0

你在哪裏設置圖像? – 2011-05-07 17:26:50

+0

via cell.imageView.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:URL]]]];我沒有在代碼中顯示 – adit 2011-05-07 17:36:39

+0

您是否改變了imageView的contentMode? – 2011-05-07 17:38:53

回答

1

代替如其他人所建議的子類化,您也可以將自己的子視圖添加到單元格的內容視圖中。

Customizing Cells

如果你想在細胞有不同 內容組件並將這些信息 在不同的位置擺出來,或者如果 要用於小區不同的行爲 特點,你有 兩種選擇。您可以將子視圖 添加到 單元格對象的contentView屬性中,或者可以創建UITableViewCell的自定義子類 。

  • 當您的內容佈局可以使用適當的自動設置設置完全指定並且不需要修改單元格的默認行爲時,您應該將子視圖添加到單元格的內容視圖。
  • 當您的內容需要自定義佈局代碼或需要更改單元的默認行爲時(例如響應編輯模式),您應該創建自定義子類。

見這個例子:

#define CUSTOM_IMAGE_TAG 99 
#define MAIN_LABEL 98 

// Customize the appearance of table view cells. 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    static NSString *CellIdentifier = @"Cell"; 
    UIImageView *customImageView = nil; 
    UILabel *mainLabel = nil; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
     customImageView = [[[UIImageView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 40.0f, 40.0f)] autorelease]; 
     customImageView.tag = CUSTOM_IMAGE_TAG; 
     [cell.contentView addSubview:customImageView]; 

     mainLabel = [[[UILabel alloc] initWithFrame:CGRectMake(60.0f, 10.0f, 100.0f, 21.0f)] autorelease]; 
     mainLabel.tag = MAIN_LABEL; 
     mainLabel.numberOfLines = 0; 
     [cell.contentView addSubview:mainLabel]; 
    } else { 
     customImageView = (UIImageView *)[cell.contentView viewWithTag:CUSTOM_IMAGE_TAG]; 
     mainLabel = (UILabel *)[cell.contentView viewWithTag:MAIN_LABEL]; 
    } 

    // Configure the cell. 
    CGRect frame = mainLabel.frame; 
    frame.size.height = ... // dynamic height 
    mainLabel.frame = frame; 

    return cell; 
} 

顯然,你還需要實現tableView:heightForRowAtIndexPath:

0

我覺得內置的ImageView的會忽視你的努力調整其大小。子類UITableViewCell並添加您自己的自定義UIImageView。然後,您可以控制圖像視圖的所有方面。

- wisenomad的解決方案將無需添加您自己的自定義圖像視圖和標籤即可使用。 -

您還必須更改textLabel的框架。這是一個例子。

- (void)layoutSubviews { 
     [super layoutSubviews]; 
     float sideLength = self.frame.size.height; 
     self.imageView.frame = CGRectMake(0.0, 0.0, sideLength, sideLength); 
     CGRect textLabelFrame = self.textLabel.frame; 
     self.textLabel.frame = CGRectMake(44.0, textLabelFrame.origin.y, textLabelFrame.size.width - 44.0 + textLabelFrame.origin.x, textLabelFrame.size.height); 
    } 
+0

I我現在得到這樣的東西skitch.com/kuntul/r6fr8/ios-simulator – adit 2011-05-07 18:54:25

+0

我稍微改變了我的例子。嘗試一下他的新代碼。一些意見是重疊的,這會導致你的問題。 – jaminguy 2011-05-07 19:40:33

2

更改單元格的子視圖框架的工作是在UITableViewCell- (void)layoutSubviews完成的,所以如果你想改變這種行爲,那麼可以繼承共同UITableViewCell,然後就不便,如:

@implementation MyTableViewCell 

- (void)layoutSubviews { 
    [super layoutSubviews]; 
    self.imageView.frame = CGRectMake(-- your own size --); 
} 

@end 
+0

所以你的意思是我創建一個新的類後,UITableViewCell的子類我需要實現layoutSubviews設置大小和一切? – adit 2011-05-07 18:37:46

+0

這是正確的。您還需要更改textLabel的框架,因爲它仍然會像大型imageView仍然存在一樣。 – jaminguy 2011-05-07 18:41:41

+0

你能告訴我一個textLabel的例子,截至目前我認爲UIImageView問題已修復,但textlabel的佈局很混亂https://skitch.com/kuntul/r6fr8/ios-simulator – adit 2011-05-07 18:46:47