2011-05-07 22 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