gpt4 book ai didi

ios - 带有以编程方式添加的图像的 UITableViewAutomaticDimension

转载 作者:可可西里 更新时间:2023-11-01 02:05:04 26 4
gpt4 key购买 nike

我对 UITableViewAutomaticDimension 有疑问:

在我的表格 View 中,我有多个以编程方式创建的 ImageView ,它们具有计算的高度:

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "managePlayset", for: indexPath) as! ManagePlaysetTableViewCell
let playset = playsets[indexPath.row]
cell.playsetName.text = playset.name
if (playset.musicItems?.count)! > 0 {
var i = 0

for musicItem in (playset.musicItems?.array)! {

let imageView = UIImageView(image: (musicItem as! MusicItem).getFirstPhoto())
imageView.frame = CGRect(x: cell.contentView.frame.origin.x+CGFloat(i)*(imageWidth+10.0), y: cell.contentView.frame.origin.y+40, width: imageWidth, height: imageWidth)
imageView.contentMode = .scaleAspectFit
cell.contentView.addSubview(imageView)
let constraint = NSLayoutConstraint(item: imageView,
attribute: NSLayoutAttribute.bottomMargin, relatedBy:
NSLayoutRelation.equal, toItem: cell.contentView,
attribute: NSLayoutAttribute.bottomMargin, multiplier: 1,
constant: 0)
cell.contentView.addConstraint(constraint)

i = i + 1
}
}

cell.editButton.tag = indexPath.row
return cell
}

在 viewDidLoad 中我设置:

let width = view.frame.width
imageWidth = width / CGFloat(maxItemsOnPage) - 10.0
tableView.estimatedRowHeight = imageWidth + 20.0
tableView.rowHeight = UITableViewAutomaticDimension

但是高度没有调整好(图片被截断)非常感谢任何建议!

最佳答案

我认为这是因为 UIImageView 是以编程方式创建的。然后它不使用自动布局系统。要更改它,您必须添加imageView.translatesAutoresizingMaskIntoConstraints = false 在你的循环中。

关于ios - 带有以编程方式添加的图像的 UITableViewAutomaticDimension,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43576502/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com