开发者

struggling with formatting dynamic row height in UITableView

I am trying to resize the height of my row in UITableView based on the text length. I have the following code:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *cellText =[[topics objectAtIndex:indexPath.r开发者_如何学编程ow] 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];
    }
}

However, it messes up with the UIImageView and the UIDetailText, image shown below:

struggling with formatting dynamic row height in UITableView

How do I fix this?

I've tried:

[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];

and none seems to work


The work of changing cell's subviews' frames is done in - (void)layoutSubviews of UITableViewCell class, so if you want alter that behavior you can subclass common UITableViewCell and then do smth like:

@implementation MyTableViewCell

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

@end


Instead of subclassing as suggested by others, you could also add your own subviews to the cell’s content view.

From Customizing Cells:

If you want the cell to have different content components and to have these laid out in different locations, or if you want different behavioral characteristics for the cell, you have two alternatives. You can add subviews to the contentView property of the cell object or you can create a custom subclass of UITableViewCell.

  • You should add subviews to a cell’s content view when your content layout can be specified entirely with the appropriate autoresizing settings and when you don’t need to modify the default behavior of the cell.
  • You should create a custom subclass when your content requires custom layout code or when you need to change the default behavior of the cell, such as in response to editing mode.

See this example:

#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;
}

Obviously, you still need to implement tableView:heightForRowAtIndexPath:.


I think the built in imageView will ignore your attempts to resize it. Subclass UITableViewCell and add your own custom UIImageView to it. Then you can control all aspects of your image view.

-- wisenomad's solution will work without having to add your own custom image view and labels. --

You will also have to change the frame of the textLabel. Here is an example.

- (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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜