【问题标题】:Scale the height of a UITableViewCell based on the table's readable width根据表格的可读宽度缩放 UITableViewCell 的高度
【发布时间】: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。有什么办法可以这样做?

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

【问题讨论】:

    标签: swift uitableview


    【解决方案1】:

    也许您应该采用屏幕或 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
        }
    }
    

    您能否附上图片以了解单元格应该是什么?

    【讨论】:

    • 不,我不想要表格的整个宽度,只想要实际显示内容的部分。我添加了一张图片来说明我的意思。
    猜你喜欢
    • 1970-01-01
    • 2011-06-21
    • 2015-09-23
    • 1970-01-01
    • 2021-02-13
    • 1970-01-01
    • 1970-01-01
    • 2011-08-06
    • 2013-11-26
    相关资源
    最近更新 更多