gpt4 book ai didi

swift - 有没有办法将长文本放入可扩展单元格的表格 View 单元格中? (以编程方式)

转载 作者:行者123 更新时间:2023-11-28 11:27:27 24 4
gpt4 key购买 nike

我的问题是我想在表格单元格中写入长标签,但我找不到任何修复方法。我试过 numberOfLines = 0, UITableView.automaticDimension 代码,但这些都不能解决我的问题。单击时我的 tableView 单元格将展开,并且将出现 cevap 标签的其他标签。展开正常工作但标签无法适应屏幕。这是我的 anchor 代码(我想我对此有疑问,但大约 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")
}

用于扩展的自定义单元格。我没有使用 Storyboard,所以我必须通过编码进行布局。如果你能帮助我,我将不胜感激。

    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 和扩展单元格,但不是我想要的。

最后的情况是:

enter image description here

最佳答案

向 cevapLabel 添加 rightbottom 约束。 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
}

关于swift - 有没有办法将长文本放入可扩展单元格的表格 View 单元格中? (以编程方式),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57786019/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com