UITableViewCell自定义内部有大量文本

时间:2018-05-02 07:01:09

标签: ios swift user-interface

我正在做一个自定义的UITableViewCell,在其中我应该输入三个文本字段,现在问题在于名为 Descirizione 的字段非常宽我应该找到一种方法来显示所有三个领域!这也让我在显示 Prera​​zzione 字段非常大的字段 Prezzo 时出现问题。

显示文字图片位于

之下

enter image description here

CustomTableViewCellArticolo.swift

import UIKit
import Foundation

class CustomTableViewCellArticolo: UITableViewCell {

    let labCodArt = UILabel()
    let labDescrizione = UILabel()
    let labPrezzo = UILabel()

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {

        super.init(style: style, reuseIdentifier: reuseIdentifier)

        labCodArt.translatesAutoresizingMaskIntoConstraints = false
        labDescrizione.translatesAutoresizingMaskIntoConstraints = false
        labPrezzo.translatesAutoresizingMaskIntoConstraints = false

        contentView.addSubview(labCodArt)
        contentView.addSubview(labDescrizione)
        contentView.addSubview(labPrezzo)

        let viewsDict = [
            "CodArt": labCodArt,
            "Descrizione": labDescrizione,
            "Prezzo": labPrezzo,
        ] as [String: Any]

        contentView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:[Prezzo]-15-[Descrizione]", options: [], metrics: nil, views: viewsDict))
        contentView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|-[CodArt]-5-[Descrizione]-|", options: [], metrics: nil, views: viewsDict))
        contentView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-[CodArt]-|", options: [], metrics: nil, views: viewsDict))
        contentView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-[Descrizione]-[Prezzo]-|", options: [], metrics: nil, views: viewsDict))
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

UIViewController.swift

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

    TableViewArticoli.beginUpdates()
    let cell = TableViewArticoli.dequeueReusableCell(withIdentifier: "MyCell", for: indexPath) as! CustomTableViewCellArticolo
    cell.labCodArt.text = filteredData[indexPath.row].CodArt
    cell.labDescrizione.text = filteredData[indexPath.row].Descrizione
    cell.labPrezzo.text = " € \(filteredData[indexPath.row].Prezzo!)"
    TableViewArticoli.endUpdates()
    return cell
}

1 个答案:

答案 0 :(得分:0)

就在这一行之后:

labPrezzo.setContentCompressionResistancePriority(UILayoutPriority.required, for: .horizontal)
labDescrizione.adjustsFontSizeToFitWidth = true

添加以下行:

{{1}}