gpt4 book ai didi

swift - 根据表格的可读宽度缩放 UITableViewCell 的高度

转载 作者:行者123 更新时间:2023-11-30 11:06:22 24 4
gpt4 key购买 nike

我有一个 UITableViewCell,其中包含一个长宽比为 4:3 的 subview ,并且应该缩放到表格的可读宽度。我像这样以编程方式构建它:

class MapCell: UITableViewCell, MKMapViewDelegate {
private let mapView = MKMapView()

init(reuseIdentifier: String?) {
super.init(style: .default, reuseIdentifier: reuseIdentifier)
self.addSubview(self.mapView)
self.mapView.translatesAutoresizingMaskIntoConstraints = false
self.addConstraints([
NSLayoutConstraint(item: self.mapView, attribute: .leftMargin, relatedBy: .equal, toItem: self, attribute: .leftMargin, multiplier: 1, constant: 0),
NSLayoutConstraint(item: self.mapView, attribute: .rightMargin, relatedBy: .equal, toItem: self, attribute: .rightMargin, multiplier: 1, constant: 0),
NSLayoutConstraint(item: self.mapView, attribute: .height, relatedBy: .equal, toItem: self.mapView, attribute: .width, multiplier: 0.75, constant: 0)
])
self.mapView.delegate = self
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}

在 View Controller 中我设置了self.tableView.cellLayoutMarginsFollowReadableWidth = true。这几乎可以工作,但我无法获得单元格的高度以正确适应 subview 。我想做这样的事情:

override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if self.getCellType(for: indexPath) == .mapCell {
return tableView.readableWidth * 0.75
} else {
return 44
}
}

显然该属性不存在,但我找不到提供我需要的值的属性 - 我检查的所有“宽度”和“大小”属性要么太大,要么为 0。有什么办法可以这样做吗?

Here is an example of the full width vs the width I want.

最佳答案

也许您应该采用屏幕或 tableView 的大小。

override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if self.getCellType(for: indexPath) == .mapCell {
return UIScreen.main.bounds.width * 0.75
} else {
return 44
}
}

您能否附上图像来了解细胞应该是什么样的?

关于swift - 根据表格的可读宽度缩放 UITableViewCell 的高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52671699/

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