我的问题是我想在表单元格中写入长标签,但是找不到任何解决方法。我尝试numberOfLines = 0,UITableView.automaticDimension代码,但是这些都不能解决我的问题。 单击时,我的tableView单元格将展开,并且将显示其他标签,即cevap标签。展开的东西可以正常工作,但标签无法放入屏幕。这是我的锚代码(我认为我对此有疑问,但大约2-3天找不到任何解决方案。)
class expandingCell: UITableViewCell {
let cellView: UIView = {
let view = UIView()
view.backgroundColor = UIColor.clear
return view
}()
let soruLabel: UILabel = {
let sorulabel = UILabel()
sorulabel.textColor = .black
sorulabel.font = UIFont.boldSystemFont(ofSize: 18)
return sorulabel
}()
let cevapLabel: UILabel = {
let cevaplabel = UILabel()
cevaplabel.textColor = .black
cevaplabel.font = UIFont.boldSystemFont(ofSize: 18)
return cevaplabel
}()
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
setup()
}
func set(content: expandingTableCellForsss) {
self.soruLabel.text = content.soru
self.cevapLabel.text = content.expanded ? content.cevap : ""
}
func setup() {
backgroundColor = UIColor.init(red: 245, green: 245, blue: 245, alpha: 1)
addSubview(cellView)
cellView.addSubview(soruLabel)
cellView.addSubview(cevapLabel)
cellView.anchor(top: topAnchor, left: leftAnchor, bottom: bottomAnchor, right: rightAnchor, paddingTop: 4, paddingLeft: 8, paddingBottom: 4, paddingRight: 8, width: frame.width, height: 60)
soruLabel.anchor(top: cellView.topAnchor, left: cellView.leftAnchor, bottom: nil, right: cellView.rightAnchor, paddingTop: 0, paddingLeft: 8, paddingBottom: 0, paddingRight: 0, width: 35, height: 35)
cevapLabel.anchor(top: soruLabel.bottomAnchor, left: cellView.leftAnchor, bottom: nil, right: nil, paddingTop: 0, paddingLeft: 8, paddingBottom: 0, paddingRight: 0, width: 0, height: 35)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
用于扩展的自定义单元格。 我没有使用情节提要,所以我必须使用编码进行布局。如果您能帮助我,我将不胜感激。
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.section == 0 {
let cell = tableView.dequeueReusableCell(withIdentifier: "details2", for: indexPath) as! customCell
let detailGelen = detailsModel[indexPath.row]
cell.detailCellLabel.text = detailGelen.itemDetailName
cell.priceLabel.text = detailGelen.itemDetailPrice!
return cell
}else {
let cell = tableView.dequeueReusableCell(withIdentifier: String(describing: expandingTableCellForsss.self), for: indexPath) as! expandingCell
cell.set(content: dataSource[indexPath.row])
return cell
}
}
此代码用于不扩展== 1和
的单元格 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
//let detailGelen = detailsModel[indexPath.row] BURADAN DATAYI ALICAK
if indexPath.section == 0 {
let cell = tableView.cellForRow(at: indexPath) as! customCell
cell.count += 1
cell.countLabel.text = "x \(cell.count)"
cell.imageForCell.image = nil
}else {
print("naptın")
let content = dataSource[indexPath.row]
content.expanded = !content.expanded
tableView.reloadRows(at: [indexPath], with: .automatic)
}
}
这是didSelectItem和扩展单元格的出现,但不是我所希望的。
最后的情况是:
答案 0 :(得分:2)
向cevapLabel添加right
和bottom
约束。 width
和height
为零。
将numberOfLines
设置为0。
使用UITableViewAutomaticDimension
。
现在cevapLabel
将根据文本设置其大小。
let cevapLabel: UILabel = {
let cevaplabel = UILabel()
cevaplabel.textColor = .black
cevaplabel.font = UIFont.boldSystemFont(ofSize: 18)
cevaplabel. numberOfLines = 0
return cevaplabel
}()
func setup() {
...
...
cevapLabel.anchor(top: soruLabel.bottomAnchor, left: cellView.leftAnchor, bottom: bottom: cellView.bottomAnchor, right: cellView.rightAnchor, paddingTop: 0, paddingLeft: 8, paddingBottom: 0, paddingRight: 8, width: nil, height: nil)
}
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
return 80 // Give estimated Height Fo rRow here
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}