简体   繁体   English

图片不适合自定义tableview单元格中的imageview

[英]Image not fit to imageview in custom tableview cell

I'm trying to load a image to my tableview cell, however, the image seems to be bigger than the imageview and exceed the boundary of imageview. 我正在尝试将图像加载到我的tableview单元格中,但是,该图像似乎比imageview大,并且超出了imageview的边界。 I tried to set the contentMode property of the UIImageView to ScaleAspectFit but it didn't solve my problem. 我试图将UIImageViewcontentMode属性设置为ScaleAspectFit但没有解决我的问题。

Code : 代码:

cell.imageView?.contentMode = UIViewContentMode.Center
cell.imageView?.contentMode = UIViewContentMode.ScaleAspectFit
switch (post.priorityLevel){
    case "High Priority Posting":
        cell.imageView?.image = UIImage(named: "High Priority Posting")
    case "Priority Posting":
        cell.imageView?.image = UIImage(named: "Priority Posting")
    case "General Posting":
        cell.imageView?.image = UIImage(named: "General Posting")
    default:
        print("Priority Level can't be loaded to table")
}

I added screenshots of my table view from Interface builder and the result on simulator. 我在“界面”构建器中添加了表格视图的屏幕截图,并在模拟器上添加了结果。

Any suggestions will be appreciated. 任何建议将不胜感激。 在此处输入图片说明

在此处输入图片说明

Follow below constraint and you will see the magic of Auto layouts 遵循以下约束,您将看到自动布局的魔力

ImageView constraints: ImageView约束:

在此处输入图片说明

Set the fix height and width you want to display for the imageView, and set VerticalCentre constraint. 设置要为imageView显示的固定高度和宽度,并设置VerticalCentre约束。 Set imageView's content mode to Aspect Fit. 将imageView的内容模式设置为Aspect Fit。

Top label constraints: 最高标签限制:

在此处输入图片说明

Label TrailingSpace constraint should not be a constant value, instead set it to standard spacing (ie 8 pts). 标签TrailingSpace约束不应为常数,而应将其设置为标准间距(即8磅)。

Second label constraints: 第二标签约束:

在此处输入图片说明

Labels TrailingSpace and BottomSpace constraint should not be a constant value, instead set it to standard spacing (ie 8 pts), and set its VerticalContentHugging priority to 250 标签TrailingSpace和BottomSpace约束不应为恒定值,而应将其设置为标准间距(即8 pts),并将其VerticalContentHugging优先级设置为250

在此处输入图片说明

Update: 更新:

ImageView vertical constraint to cell: ImageView对单元格的垂直约束:

在此处输入图片说明

Label1 TrailingSpace to cell with standard spacing: Label1 TrailingSpace到具有标准间距的单元格:

在此处输入图片说明

Label2 TrailingSpace to cell with standard spacing and BottomSpace to cell with standard spacing. Label2将TrailingSpace移至具有标准间距的单元格,并将BottomSpace移至具有标准间距的单元格。

在此处输入图片说明

尝试cell.imageView?.contentMode = UIViewContentMode.ScaleAspectFill

https://developer.apple.com/library/ios/referencelibrary/GettingStarted/DevelopiOSAppsSwift/Lesson7.html#//apple_ref/doc/uid/TP40015214-CH8-SW1 https://developer.apple.com/library/ios/referencelibrary/GettingStarted/DevelopiOSAppsSwift/Lesson7.html#//apple_ref/doc/uid/TP40015214-CH8-SW1

In this official tutorial, they design a tableview exactly like yours. 在此官方教程中,他们设计与您完全一样的表格视图。 I think your main issue has to do with the lack of constraints/autolayout 我认为您的主要问题与缺乏约束/自动布局有关

UIImage UIImage

1) first you set the imageview leading space ,equal height,center vertically to container 2)set height itself 1)首先设置imageview的前导空间,等高,垂直于容器的中心2)设置高度本身

City and State 城市和州

1) set horizontal space and Top to UiimageView, Trailing space to container view 2)set height itself 1)将水平空间和顶部设置为UiimageView,将空间拖到容器视图2)设置高度本身

Description label 说明标签

1)set horizontal space and Bottom to UiimageView, Trailing space to container view 1)将水平空间和底部设置为UiimageView,将空间拖到容器视图

2)set height itself story board -> set lines = 0 2)设置故事板本身的高度->设置线= 0

If its a custom table view cell, dont name the imageView outlet to "imageView", name it to any other for eg: "cellImageView". 如果是自定义表格视图单元,则不要将imageView出口命名为“ imageView”,而将其命名为其他名称,例如:“ cellImageView”。 and then try setting clipsToBound to true of your cell cellImageView. 然后尝试将单元格cellImageView的clipsToBound设置为true。

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

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