我有一个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
    }
}显然,该属性不存在,但我找不到一个提供所需值的属性-我检查的所有"width“和"size”属性要么太大,要么为0。有没有办法做到这一点?
发布于 2018-10-06 06:39:16
也许你应该看看屏幕或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
    }
}你能附加图像来理解细胞应该是什么吗?
https://stackoverflow.com/questions/52671699
复制相似问题