简体   繁体   English

使用故事板中的AutoLayout在表格视图单元格中的图像

[英]Image in Table View Cell using AutoLayout in Storyboard

I have a Table View Cell , and all that is in it is an ImageView . 我有一个Table View Cell ,其中的所有内容都是ImageView

How would I get the Table View Cell size to change dynamically with the Image (sizes) I get from Instagram's API ? 如何使用Instagram的API获得的图像(大小)动态更改表格视图单元格大小

I'm having a hard time solving, because there are essentially two variables I'm solving for: 我很难解决,因为我正在解决的基本上有两个变量:

  • Variable Size of Image from Instagram API (standard res always seem to be at least 600+ so will always be able to stretch from left to right no matter what phone size) 来自Instagram API的可变尺寸图像 (标准res似乎总是至少600+,因此无论手机大小如何,总是能够从左到右延伸)
  • Variable iPhone Size (iPhone 6, 6 Plus, etc) 可变iPhone尺寸 (iPhone 6,6 Plus等)

I've tried pinning all sides, that didn't work. 我试过钉住所有方面,但没有用。 I've tried pinning left and right along with add constraints Bottom Space to Container Margin and Top Space to Container Margin, that didn't work. 我已经尝试左右固定,同时添加限制底部空间到容器边距和顶部空间到容器边距,这不起作用。

But I can't get it right. 但我无法做到对。 I'm sure its something small I'm missing. 我确定它有点小我不见了。 Any help would be appreciated. 任何帮助,将不胜感激。 Thanks! 谢谢!

I'm using Xcode 6 and iOS 8. I'm using Storyboard. 我正在使用Xcode 6和iOS 8.我正在使用Storyboard。 Only Portrait orientation. 仅纵向方向。

I don't know whether this can be done in the storyboard or not. 我不知道这是否可以在故​​事板中完成。 Maybe there's a way to do it in iOS 8 using self-sizing cells, but I didn't find the way to do that, and in any case, that would only work for iOS 8, not 7. Anyway, it's easy enough to do with code, and simple constraints setup in the storyboard. 也许有一种方法可以在iOS 8中使用自行调整大小的单元格,但我没有找到方法,在任何情况下,这只适用于iOS 8,而不是7.无论如何,它很容易使用代码,并在故事板中设置简单的约束。 Just pin your image view to all four sides of the cell (I did it in my example using the standard margins of 8 to all sides), and then implement heightForRowAtIndexPath, 只需将图像视图固定到单元格的所有四个边(我在我的示例中使用8到所有边的标准边距),然后实现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;
}

The 16's in the code are to account for the padding of 8 on all the sides of the image view. 代码中的16位是在图像视图的所有边上考虑填充8。 If you have your image view go all the way to the left and right edges, then you can eliminate those. 如果您的图像视图一直到左右边缘,那么您可以消除这些。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM