【发布时间】:2018-10-05 18:49:59
【问题描述】:
我有一个UITableViewCell,它包含一个具有 4:3 纵横比的子视图,并且应该缩放到表格的可读宽度。我像这样以编程方式构建它:
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")
}
}
在视图控制器中我设置了self.tableView.cellLayoutMarginsFollowReadableWidth = true。这几乎可以工作,但我无法让单元格的高度正确适合子视图。我想做这样的事情:
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if self.getCellType(for: indexPath) == .mapCell {
return tableView.readableWidth * 0.75
} else {
return 44
}
}
显然该属性不存在,但我找不到提供所需值的属性 - 我检查的所有“宽度”和“大小”属性都太大或 0。有什么办法可以这样做?
【问题讨论】:
标签: swift uitableview