user1060418
user1060418

Reputation: 215

ios TableView different cell height

I am working on an app which requires me to show pictures and some text in TableView. These pictures can be of different heights and so I need to vary the cell height accordingly. so I have overridden this method :

 - (CGFloat)tableView:(UITableView *)tableView
heightForRowAtIndexPath:(NSIndexPath *)indexPath

If I have a single static value for cell identifier then the height of the image inside the cell cannot vary dynamically.

So do I need to have different values of Cell Identifier for each cell ? Is there some other way ?

I cannot use some other view than Tableview because I need to show some cells dynamically in between based on user interaction.

Thanks.

Upvotes: 5

Views: 4519

Answers (5)

user5466854
user5466854

Reputation:

For dynamic tableViewCell height use this.

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
  CGSize labelHeight = [self heigtForCellwithString:yourLabel.text    withFont:yourLabel.font];
    return labelHeight.height; // the return height + your other view height
}

-(CGSize)heigtForCellwithString:(NSString *)stringValue withFont:(UIFont)font{
 CGSize constraint = CGSizeMake(300,9999); // Replace 300 with your label width
  NSDictionary *attributes = @{NSFontAttributeName: font};
  CGRect rect = [stringValue boundingRectWithSize:constraint
                                       options:         (NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading)
                                    attributes:attributes
                                       context:nil];
    return rect.size;

}

Upvotes: 1

Busen
Busen

Reputation: 29

You should calculate the height of each cell before the cell display.So,You could write a method to get the height,and then use the method you written in the Model.

Upvotes: 0

moosa0709
moosa0709

Reputation: 476

Calculate the Cell Height

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
{
  NSString *text = [items objectAtIndex:[indexPath row]];

  CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f);

  CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];

  CGFloat height = MAX(size.height, 44.0f);

  return height + (CELL_CONTENT_MARGIN * 2);
}

For more reference see the Link..

Upvotes: 0

Hobbes the Tige
Hobbes the Tige

Reputation: 3821

You should reference your data model to get the image size and return the row height.

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
        UIImage *image = dataModel[indexPath.row];
        return image.size.height + 16.0; //16 is for padding
}

If you subclass your cell, you can adjust the imageView frame in -layoutsubviews (after super):

- (void)setupImageView
{
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectZero];
    imageView.contentMode = UIViewContentModeScaleAspectFit;
    imageView.clipsToBounds = YES;
    self.contentImageView = imageView;

    [self.contentView addSubview:imageView];
}

- (void)layoutSubviews
{
    [super layoutSubviews];

    CGRect bounds = self.contentView.bounds;
    float margin = 8.0;
    CGRect textViewFrame = CGRectZero;
    textViewFrame = CGRectMake(margin, roundf(margin * 2.0), bounds.size.width - (margin * 2.0), roundf(bounds.size.height - (margin * 4.0)));

    self.contentImageView.frame = textViewFrame;
}

Upvotes: 2

Refael.S
Refael.S

Reputation: 1644

Example for only an image:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    //get reference to a image
    UIImage *image = [arrImages objectAtIndex:indexPath.row];

    //get the height of your imageview on the cell
    CGFloat imageViewHeight = image.size.height/2;//RetinaDisplay

    //return the height of the imageview (plus some padding) for your cell height
    return imageViewHeight; //add here also all other constant height's of objects that you have on your cell.
}

For a more comprehensive example look at my answer here:

Resize Custom cell with a UILabel on it based on content text from JSON file

Upvotes: 0

Related Questions