有没有办法将长文本放入可扩展单元格的表格视图单元格中? (以编程方式)
Is there a way to fit long texts inside the tableview cell which is expandable cell? (programmatically)
我的问题是我想在 table 单元格中写入长标签,但我找不到任何修复方法。我尝试了 numberOfLines = 0, UITableView.automaticDimension 代码,但是 none 这解决了我的问题。
单击时我的 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 和扩展单元格,但不是我想要的。
最后的情况是:
enter image description here
向 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
}
我的问题是我想在 table 单元格中写入长标签,但我找不到任何修复方法。我尝试了 numberOfLines = 0, UITableView.automaticDimension 代码,但是 none 这解决了我的问题。 单击时我的 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 和扩展单元格,但不是我想要的。
最后的情况是:
enter image description here
向 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
}