使自定义TableViewCell的UIimageView宽度为单元格宽度的1/3,并且单元格的高度=图像宽高比

时间:2018-08-05 21:38:09

标签: ios swift uitableview

我是iOS编程的新手,我主要是在使用界面生成器来调整单元格布局。 目前,我有一个带有ImageView和Label的自定义TableView单元。

我想:

1)具有ImageView Width =单元格大小的1/3(看来我无法通过情节提要来做到这一点)

2)在保持图像高宽比的同时,动态调整像元高度。我已经为UIImageView添加了等于单元格高度的约束。

我搜索了StackOverflow,但其中一些答案确实非常复杂,因此我无法真正理解如何使其适合我的情况。 我在限制条件下玩耍,但似乎必须以编程方式执行此操作?

这是我目前使用的单元格方法:

extension FirstTableViewController: UITableViewDelegate, UITableViewDataSource{

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return dataCells.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let folderCell = dataCells[indexPath.row]
        print(folderCell)
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! FirstTableViewCell
        cell.setCell(cell:folderCell)
        return cell

这是我的细胞班:

class FirstTableViewCell: UITableViewCell {


    @IBOutlet weak var ImageView: ScaledHeightImageView!
    @IBOutlet weak var FolderLabel: UILabel!

    override func awakeFromNib() {
        super.awakeFromNib()
        //print(FolderLabel)


    }
    func setCell (cell : FolderCell){
        self.ImageView.image = cell.image
        self.FolderLabel.text = cell.label
    }
}

1 个答案:

答案 0 :(得分:1)

您可以从情节提要中添加具有相等宽度的Superview,然后将其乘数更改为0.3 enter image description here

enter image description here