Image in View Cell table using AutoLayout in storyboard

I have , and all that is in it is this . Table View Cell

ImageView

How can I get the cell size of the Table View to dynamically resize the image (s) I get from the Instagram APIs ?

I find it difficult to solve problems because there are two variables that I solve for:

  • Resized image from Instagram APIs (standard res is always seemingly at least 600+ so it will always be able to stretch from left to right regardless of phone size)
  • Resized iPhone (iPhone 6, 6 Plus, etc.)

I tried attaching all sides, it didn't work. I tried snapping left and right along with the additional Bottom Space constraints on the Container Margin and Top Space on the Container Margin which didn't work.

But I cannot understand. I'm sure there is something small I am missing. Any help would be greatly appreciated. Thank!

I am using Xcode 6 and iOS 8. I am using Storyboard. Portrait orientation only.

+3


source to share


1 answer


I don't know if it can be done in storyboard or not. There might be a way to do this in iOS 8 using self-calibrate cells, but I haven't found a way to do this, and this will only work for iOS 8, not 7 anyway. Anyway, it's easy enough to do with code and set simple constraints in the storyboard. Just attach the image to all four sides of the cell (I did this in my example using standard 8 margins for all sides) and then applied heightForRowAtIndexPath.

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    CGSize imageSize = [[UIImage imageNamed:self.theData[indexPath.row]] size];
    CGFloat height = imageSize.height/imageSize.width * (tableView.frame.size.width - 16);
    return height + 16;
}

      



16 in the code should take into account padding 8 on all sides of the image. Once you have a view of the image, go to the left and right edges, you can eliminate them.

+1


source







All Articles