0

I'm new to iOS development.. and I'm stuck with the problem of changing table view height dynamically.

I know that I should do it in tableView:heightForRowAtIndexPath: method but[cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height content is always returning one small value 18.0..

My cell layout looks like this:

with such scene:

Here is the code in my TableViewController:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
   MYModel *model = [[MYModel alloc] initWithDictionary:[self.items objectAtIndex:indexPath.row]];
   NSString *reuseIdentfier = @"textCell";
   MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentfier forIndexPath:indexPath];
   [self configureCell:cell forRowAtIndexPath:indexPath withData:model];

   return cell;
}

I'm using SDWebImage (https://github.com/rs/SDWebImage) for loading images and here is the configure cell method below:

- (void)configureCell:(MyTableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath withData:(MYModel *)model 
{

   ANOTHERModel *itemCollection = [[ANOTHERModel alloc] initWithDictionary:model.collection];

   cell.collectionTitle.text = [itemCollection title];
   cell.collectionOwner.text = [NSString stringWithFormat:@"by %@", itemCollection.ownerName];
   [cell.collectionOwnerAvatarView setImageWithURL:[NSURL URLWithString:itemCollection.ownerAvatar] placeholderImage:[UIImage imageNamed:@"gravatar.png"]];

   // make circle avatars
   CALayer *layer = [cell.collectionOwnerAvatarView layer];
   [layer setMasksToBounds:YES];
   [layer setCornerRadius:cell.collectionOwnerAvatarView.frame.size.height/2];

   // type
   NSString *imageName = [model.type stringByAppendingFormat:@".png"];
   UIImage *typeImage = [UIImage imageNamed:imageName];
   [cell.typeIconView setImage:typeImage];

   // title and description
   cell.itemTitle.text = [model title];
   cell.itemDescription.text = [model description];

   [cell.itemDescription setTextAlignment:NSTextAlignmentLeft];
   [cell.itemDescription setLineBreakMode:NSLineBreakByWordWrapping];
   [cell.itemDescription setNumberOfLines:0];

   // always returns 18.0
   NSLog(@"%f", [cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height);  
}

I really don't understand why.. please help!

Thanks!

Kosmetika
  • 20,774
  • 37
  • 108
  • 172
  • Did you try to set the frame of the UITableviewCell ? – BoilingLime May 02 '14 at 15:22
  • Nope.. could you show me how to do it? – Kosmetika May 02 '14 at 15:43
  • Does this give the desired result - NSLog(@"%f", [cell.itemDescription systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height); Check out this link - http://stackoverflow.com/questions/22621586/cell-contentview-systemlayoutsizefittingsize-does-not-work-for-dynamic-height-t – rshankar May 02 '14 at 15:46
  • ``[cell.itemDescription systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height`` returns actual heights.. – Kosmetika May 02 '14 at 16:07

0 Answers0