首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >根据表格的可读宽度缩放UITableViewCell的高度

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

Stack Overflow用户
提问于 2018-10-06 02:50:00
回答 1查看 62关注 0票数 0

我有一个UITableViewCell,它包含一个具有4:3纵横比的子视图,并且应该缩放到表格的可读宽度。我以编程方式构建它,如下所示:

代码语言:javascript
运行
复制
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。这几乎可以工作,但我不能获得适合子视图的单元格的高度。我想做这样的事情:

代码语言:javascript
运行
复制
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。有没有办法做到这一点?

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

EN

回答 1

Stack Overflow用户

发布于 2018-10-06 06:39:16

也许你应该看看屏幕或tableView的大小。

代码语言:javascript
运行
复制
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
    }
}

你能附加图像来理解细胞应该是什么吗?

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52671699

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档