我正在做一个自定义UITableViewCell,我应该在其中输入三个文本字段,现在的问题在于,名为Descirizione的字段确实非常宽,我应该找到一种方式来显示所有三个字段!这也给我带来了一个问题,即当描述Descrizione很大时,显示的Prezzo字段会移动。
显示文字图片如下
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
}
最佳答案
在此行之后:
contentView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-[Descrizione]-[Prezzo]-|", options: [], metrics: nil, views: viewsDict))
添加此行:
labPrezzo.setContentCompressionResistancePriority(UILayoutPriority.required, for: .horizontal)
labDescrizione.adjustsFontSizeToFitWidth = true
关于ios - UITableViewCell自定义,其中包含大量文本,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50128821/