简体   繁体   English

像元高度等于图像宽度

[英]Cell height equal to image width

I've created cell with 4 different imageViews where i've created dynamic constraints, so they will change the width and height equal to the device size, but since cell is not dynamic then it won't change the height of the imageView. 我创建了具有4个不同imageViews的单元格,并在其中创建了动态约束,因此它们将更改与设备大小相等的宽度和高度,但是由于单元格不是动态的,因此它不会更改imageView的高度。 Therefore I want to set the cell Height equal to the image Width, so the images always will have the same width and height. 因此,我想将单元格高度设置为等于图像宽度,以便图像始终具有相同的宽度和高度。

In heightForRowAtIndexPath, I've tried to return following, but this is giving me an error: (lldb) 在heightForRowAtIndexPath中,我尝试返回以下内容,但这给我一个错误:(lldb)

let cell = tableView.dequeueReusableCellWithIdentifier("ImageViewCell", forIndexPath: indexPath) as ImageViewCell

return cell.image1.frame.width

How can I do this? 我怎样才能做到这一点?

I think you need to use to Tableview's Delegate to set its cell height. 我认为您需要使用Tableview的Delegate来设置其单元格高度。 maybe you can try somethin like that: 也许您可以尝试这样的事情:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
     switch (indexPath.row) {
    case 0:
        return image.frame.size.width;
        break;
    case 1:
        return image.frame.size.width;
        break; 
    default:
        return 44;
        break;
     }
}

I think you need to get the size of UIimage from your datasource. 我认为您需要从数据源获取UIimage的大小。 not from your cell. 不是从你的手机上来的。

you cant call cell object within heightForRowAtIndexPath because it creates infinite loop as cellForRowAtIndexPath calls heightForRowAtIndexPath . 您不能在heightForRowAtIndexPath调用单元格对象,因为它会在cellForRowAtIndexPath调用heightForRowAtIndexPath时创建无限循环。 secondly, adjust width using constraints as function name clearly indicates that it is for height. 其次,使用约束来调整width因为函数名称清楚地表明它是针对高度的。

If you are sure about the height of the control, then use this 如果您确定控件的高度,请使用此控件

return 90.0 //its optional if height is fixed 

else 其他
add constraints for top , bottom , left , right , width and height constraint, and use >= condition for height constraint. topbottomleftrightwidthheight约束添加constraints ,并对height约束使用> =条件。

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

相关问题 iOS图像视图 - 高度等于宽度的大小 - iOS Image View - Height equal to the size of width iOS设置单元格高度取决于URL图像高度宽高比 - IOS setting Cell height to depend on url image height width ratio 为不同的 iOS 设备创建等高和等宽的圆形图像时出错 - Error in Creating a Rounded Image of Equal Height and Width for Different iOS Devices 自动布局:具有相等宽度/高度的正方形图像视图 - Auto Layout: Square Image View with equal width / height 如何在自定义单元格中使UIImage的宽度等于视图的宽度和比例高度 - How to make UIImage width be equal to view's width and proportional height inside a custom cell 如何在ios中获取放置在自定义单元格中的图像的高度和宽度 - How to get the height and width of the image placed in a custom cell in ios 计算UITableViewCell的高度,该高度取决于单元格宽度和异步加载的图像 - Calculating height of the UITableViewCell that is dependent on the cell width and image loaded asynchronously 像元高度作为图像高度 - cell height as image height 如何在 StackView 中设置等宽或等高? - How to Set Equal Width or Equal Height in StackView? 使自定义TableViewCell的UIimageView宽度为单元格宽度的1/3,并且单元格的高度=图像宽高比 - Have UIimageView width of custom TableViewCell be 1/3 of cell width, and height of cell = image aspect ratio
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM